Javascript和此错误.取决于函数的调用方式
Javascript and this error. Depending on how function called
所以这很难。当直接调用一个方法时,这是指包含的对象,而当从字符串转换调用时,这指的是"窗口"(我相信,我仍然可能完全错了)。
有人能向我解释一下吗?
将发生的是在第一条警报语句之后的javascript错误(这意味着直接调用与_this
的定义一样正常)
/**
* converts a string to a function
*/
function stStringToFunction(string) {
var fnList = string.split(".");
var currentFn = window;
var nextFn;
while(currentFn !== undefined && (nextFn = fnList.shift())) {
currentFn = currentFn[nextFn];
}
return currentFn;
}
//An example library
UnderstandingThis.ui.Component1 = {
performTask1: function(options) {
var settings = {
opt1: true,
opt2: false
},
_this = this;
$.extend(settings, options);
_this._PrivateTask1(settings);
},
_PrivateTask1: function(settings) {
//Some sweet stuff here!
alert("Private task1: " + settings.from);
}
}
UnderstandingThis.ui.Component1.performTask1({from: "direct"});
stStringToFunction("UnderstandingThis.ui.Component1.performTask1")({from: "string-to-function"});
我没有解释需要编辑:2/21/2012上午9:56(蒙大拿州)
因此,问题是伪库(UnderstandingThis.ui.Component1
)包含一个使用_this
(设置为this
)的函数,并且在执行直接方法调用时工作良好。我的意思是在javascript中调用UnderstandingThis.ui.Component1.performTask1()
。但如果我通过字符串转换调用相同的任务stStringToFunction("UnderstandingThis.ui.Component1.performTask1")()
,则_this
指的是window
。但对我来说,这毫无意义。我做javascript才1.5年,这些细微差别(来自Java/C#)很难理解。
如果您调用:
foo.bar()
那么在bar
内部,this
就是foo
。
如果您:
var baz = { bar: foo.bar };
baz.bar();
那么在bar
内部,this
就是baz
。
如果你要:
var bar = foo.bar;
bar();
然后在bar
内部,this
就是window
(因为它是默认对象)。
函数从上下文中提取函数,然后调用它(因此this
就是window
)。
您的初始断言:
当直接调用一个方法时,这指的是包含对象
不是真的。this
伪变量基于用于获取函数(如果有的话)的对象引用进行绑定。如果您将函数实例化为某个对象的属性值,然后通过该对象属性通过引用调用它,那么是的,this
绑定到该对象。但是,如果将函数作为值复制到另一个对象的属性中,则通过该另一个对象的调用将使该对象为this
(在讨论此主题时,这始终是"谁先上?"的问题:-)。
在任何情况下,您都可以使用.call()
或.apply()
来强制this
值为您想要的值,而不管您如何访问该函数。底线是,在JavaScript中,函数和任何特定对象之间从来没有任何固有的关系。CCD_ 26的绑定是在每次函数调用时确定的。
问题正是您所想的,这在第二个调用中指的是窗口。这里有一个可能的变通办法,但就我个人而言,我会用不同的方式做事。Fiddle herehttp://jsfiddle.net/LsH7V/
/**
* converts a string to a function
*/
function stStringToFunction(string) {
var fnList = string.split(".");
var currentFn = window;
var nextFn;
while(currentFn !== undefined && (nextFn = fnList.shift())) {
currentFn = currentFn[nextFn];
}
return currentFn;
}
//An example library
UnderstandingThis = {};
UnderstandingThis.ui = {};
UnderstandingThis.ui.Component1 = {
performTask1: function(options) {
var settings = {
opt1: true,
opt2: false
};
if(typeof _this === 'undefined'){
_this = this;
console.log(this);
}
$.extend(settings, options);
_this._PrivateTask1(settings);
},
_PrivateTask1: function(settings) {
//Some sweet stuff here!
alert("Private task1: " + settings.from);
}
}
UnderstandingThis.ui.Component1.performTask1({from: "direct"});
stStringToFunction("UnderstandingThis.ui.Component1.performTask1")({from: "string-to-function"});
- 任何方式使AJAX调用Gmail API,而无需通过JS库
- 在phonegap中为android调用onload函数的最佳方式
- 使用Angular存储用户以前是否选中过复选框,然后再调用它的最佳方式是什么
- 如何用JavaScriptEasy的方式一次调用大量restful服务
- 以不同的方式调用javascript函数
- 以编程方式调用javascript函数
- 以 func(a)(b)(c) 的方式调用的 Javascript 函数
- waht是将数组从jqueryajax调用传递到servlet的最佳方式
- 如何用两种不同的方式调用同一个函数
- 在没有JQuery的情况下加载HTML主体之后调用JavaScript脚本的最佳方式
- 以编程方式调用模糊会禁用与浏览器窗口的交互
- 以编程方式调用选项卡捕获引发异常
- js 函数的行为因调用方式而异
- Select2 以编程方式设置搜索词并打开包含相关结果的下拉列表(无 ajax 调用)
- 调用后端时,页面会失去对齐方式,并且需要一些时间来重新对齐控件
- 防止以编程方式调用 django-rest-framework API
- 调用事件侦听器 - 两种方式之一起作用,有什么区别
- JavaScript 对象,使用两种不同的调用函数方式查看预期结果
- Javascript”;这个“;作用域根据调用方式给出不同的结果
- Javascript和此错误.取决于函数的调用方式