Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript;这";调用方法中的对象_Javascript - Fatal编程技术网

javascript;这";调用方法中的对象

javascript;这";调用方法中的对象,javascript,Javascript,我创建这些函数是为了检查每个函数中的“this”是什么,并且想知道当使用call函数时,为什么“this”不是“test”对象 如果执行foo.call(test),则在foo()的内部,此将是test 但是,在foo()内部,当您随后调用bar()时,此值将在bar()内部重置为其默认值 Javascript中的每个函数调用都会根据函数的调用方式设置一个新值this 普通函数调用(如bar())将this的值设置为全局对象(如果未在严格模式下运行)或未定义(如果运行严格模式) 要查看,将此值设

我创建这些函数是为了检查每个函数中的“this”是什么,并且想知道当使用call函数时,为什么“this”不是“test”对象

如果执行
foo.call(test)
,则在
foo()
的内部,
将是
test

但是,在
foo()
内部,当您随后调用
bar()
时,
值将在
bar()
内部重置为其默认值

Javascript中的每个函数调用都会根据函数的调用方式设置一个新值
this

普通函数调用(如
bar()
)将
this
的值设置为全局对象(如果未在严格模式下运行)或
未定义(如果运行严格模式)

要查看,将
值设置为特定值的方法有:

foo.call(test, ...);      // sets this to test
foo.apply(test, ...);     // sets this to test

obj.method();             // sets this to obj

var x = foo.bind(test);   
x();                      // sets this to test

var x = new foo();        // create a new object and sets this to the new object

foo();                    // this set to global object or undefined (in strict mode)

请记住,
this
的值根据上述规则在Javascript中的每个函数调用时都会重置。它永远不会从上一个值“继承”。根据函数调用的方式,每次函数调用都会将其设置为新的值。对于不熟悉Javascript的人来说,这是一个常见的误解(许多月前我也被同样的事情绊倒了)

还值得注意的是,在调用回调函数时,回调函数可能会将
this
的值设置为特定的值(内部使用。
apply()
.call()
)。例如:

function submitHandler(e) {
   // value of this is in this function is set
   // by addEventListener to the DOM object that handled the event
}

document.getElementById("submit").addEventListener("click", submitHandler);

关于该主题的其他参考资料:

如果您执行
foo.call(test)
,则在
foo()的内部,
将是
test

但是,在
foo()
内部,当您随后调用
bar()
时,
值将在
bar()
内部重置为其默认值

Javascript中的每个函数调用都会根据函数的调用方式设置一个新值
this

普通函数调用(如
bar()
)将
this
的值设置为全局对象(如果未在严格模式下运行)或
未定义(如果运行严格模式)

要查看,将
值设置为特定值的方法有:

foo.call(test, ...);      // sets this to test
foo.apply(test, ...);     // sets this to test

obj.method();             // sets this to obj

var x = foo.bind(test);   
x();                      // sets this to test

var x = new foo();        // create a new object and sets this to the new object

foo();                    // this set to global object or undefined (in strict mode)

请记住,
this
的值根据上述规则在Javascript中的每个函数调用时都会重置。它永远不会从上一个值“继承”。根据函数调用的方式,每次函数调用都会将其设置为新的值。对于不熟悉Javascript的人来说,这是一个常见的误解(许多月前我也被同样的事情绊倒了)

还值得注意的是,在调用回调函数时,回调函数可能会将
this
的值设置为特定的值(内部使用。
apply()
.call()
)。例如:

function submitHandler(e) {
   // value of this is in this function is set
   // by addEventListener to the DOM object that handled the event
}

document.getElementById("submit").addEventListener("click", submitHandler);

关于该主题的其他参考资料:


谢谢您的回答!!我能想象一下作用域链的结果吗:如果做foo.call(test),那么foo()的内部,“this”将是test.,而foo()的内部,当调用bar()时,因为测试对象没有bar()方法,所以它会搜索作用域链并在window对象中找到它。(对不起,我的英语表达不好)谢谢你的回答!!我能想象一下作用域链的结果吗:如果执行foo.call(test),那么在foo()的内部,“this”将是test.,而在foo()的内部,当调用bar()时,因为测试对象没有bar()方法,所以它将搜索作用域链,并在window对象中找到它。(对不起,我的英语表达不好)