Javascript和此错误.取决于函数的调用方式

Javascript and this error. Depending on how function called

本文关键字:调用 方式 函数 取决于 错误 Javascript      更新时间:2023-09-26

所以这很难。当直接调用一个方法时,这是指包含的对象,而当从字符串转换调用时,这指的是"窗口"(我相信,我仍然可能完全错了)。

有人能向我解释一下吗?

将发生的是在第一条警报语句之后的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"});