1. 程式人生 > >函數易錯題目合集

函數易錯題目合集

ole this 合集 對象 bsp ict define style ber

函數易錯題目合集

var f1 = function f2(){};  
f1.name  //"f2"


var f1 = function f2(){};  
f2.name  //報錯:f2 is not defined

var f1 = function f2(){}; 
console.log(f2)  
//f2 is not defined(f2 不存在,而且 f2 不是 undefined)

function f(){console.log(this)}; 
f.call(1)  //Number 對象 1

function f(){use strict console.log(this)}; 
f.call(
1) //1 function f(){console.log(this)}; f.call() //Window 對象 function f(){use strict console.log(this)}; f.call() //undefined var a = console.log(1); a //undefined function f(){ return 1}; a = f ; a //函數 f function f(){ return 1}; var a = f.call() ; a //1 var a = 1,2; a //報錯 var a = (1,2); a
//2 var a = (1, console.log(2)); a //undefined function f(){ return function f2(){} } var a = f.call() a //函數 f2 function f(){ return function f2(){} } var a = f.call() var b = a.call() b //undefined function f1(){ console.log(this) function f2(){ } } var obj = {name: obj} f1.call( obj )
//obj 對象 function f1(){ function f2(){ console.log(this) } f2.call() } var obj = {name: obj} f1.call( obj ) //Window 對象 function f1(){ console.log(this) // 第一個 this function f2(){ console.log(this) // 第二個 this } f2.call() } var obj = {name: obj} f1.call( obj ) //為什麽兩個 this 值不一樣? //this 就是 call 的第一個參數,第一個 this 對應的 call 是 f1.call(obj),第二個 this 對應的 call 是 f2.call() //this 和 arguments 都是參數,參數都要在函數執行(call)的時候才能確定

函數易錯題目合集