【问题标题】:Why do I need to use the "this" keyword when accessing a parameter for an anonymous self-executing function?为什么在访问匿名自执行函数的参数时需要使用“this”关键字?
【发布时间】:2017-03-02 19:34:48
【问题描述】:

我刚刚开始学习 Javascript,并且一直在玩匿名自执行函数。我编写了一些无法按预期方式工作的代码。为什么在这种情况下需要“this”关键字来获取变量“shoutIt”的值?

第一个警报显示“它有效吗?(1) 未定义”,而第二个警报显示“它有效吗?(2) [YES!]”。

谢谢!

var shoutIt = "[YES!]";

//creating an anonymous, self-executing function
(
    function (shoutIt) {
        shoutItDebug = shoutIt;
        shoutItDebug = this.shoutIt;
        alert("Did it work? (1) " + shoutIt); //doesn't work, undefined
        alert("Did it work? (2) " + this.shoutIt) //works
})();

【问题讨论】:

  • 在您的代码中,this 不是函数,this 是全局范围。将console.log(this) 放入您的函数并检查输出。
  • 你应该使用严格模式,然后它不会按预期工作。
  • 我不知道有不同的模式。我会打开严格的,谢谢。

标签: javascript anonymous-function


【解决方案1】:

这里有两个变量叫shoutIt:一个是var shoutIt定义的全局变量,另一个是function (shoutIt) { ...中的形参定义的变量

当你在非严格模式下运行一个非方法函数(即foo()而不是bar.foo())时,this等于全局对象(在浏览器中,window) .在函数内部,this.shoutIt 指的是全局范围内的shoutIt 变量。

相比之下,shoutIt 在这里指的是具有该名称的函数参数,而不是全局变量,它是作用域上一级。 (全局变量被同名的更直接变量“遮蔽”。)该函数不使用任何参数调用,因此函数中shoutIt 的值是undefined。

如果您想传入一个值以用作名为 shoutIt 的参数,请在调用括号中提供一个:

(function (shoutIt) {
    ...
})(someValue);

【讨论】:

    【解决方案2】:

    因为您的匿名函数期望 shoutIt 作为参数,但您没有向它传递任何内容。

    基本上你的函数需要一个参数shoutIt。此参数将在函数的本地范围内。如果没有传递任何内容,并且当编译器将获取 shoutIt 的值时,它将立即从本地范围访问它并且不会进入全局级别。在本地级别,因为您没有向函数传递任何内容,所以它会为您提供undefined

    有两种解决方法

    1) 传递shoutIt的值

    var shoutIt = "[YES!]";
    
    //creating an anonymous, self-executing function
    (
        function (shoutIt) { //expecting that value
            shoutItDebug = shoutIt;
            shoutItDebug = this.shoutIt;
            alert("Did it work? (1) " + shoutIt);
            alert("Did it work? (2) " + this.shoutIt)
    })(shoutIt);**strong text** //passing argument to the function
    

    或

    2) 不传递任何参数

    var shoutIt = "[YES!]";
    
    //creating an anonymous, self-executing function
    (
        function () {
            shoutItDebug = shoutIt;
            shoutItDebug = this.shoutIt;
            alert("Did it work? (1) " + shoutIt); 
            alert("Did it work? (2) " + this.shoutIt) 
    })();
    

    “这个”的工作原理

    基本上,“this”指的是 javascript 中的上下文。 默认情况下,它指向窗口对象。尝试做

    console.log(this) //outputs the window object
    

    在全局级别定义的任何内容都会自动链接到窗口对象。

    【讨论】:

    • 谢谢,现在我只是没有像我想要的那样传递参数,这是有道理的。不知道如何选择这个作为我的答案,但它帮助了我很多! :)
    • 很高兴我能帮助@AustinGerschler
    【解决方案3】:

    您的参数与函数外部的变量命名相同,并且您没有将变量传递给函数。

    通过您的示例,您可以做一些不同的事情来使其按预期工作。

    A.将shoutIt传递给函数

    var shoutIt = "[YES!]";
    //creating an anonymous, self-executing function
    (
        function (shoutIt) {
            shoutItDebug = shoutIt;
            shoutItDebug = this.shoutIt;
            alert("Did it work? (1) " + shoutIt); //doesn't work, undefined
            alert("Did it work? (2) " + this.shoutIt) //works
    })(shoutIt);
    

    B.更改函数定义中的参数名称。

    var shoutIt = "[YES!]";
    
    //creating an anonymous, self-executing function
    (
        function (shoutItAgain) {
            shoutItDebug = shoutIt;
            shoutItDebug = this.shoutIt;
            alert("Did it work? (1) " + shoutIt); //doesn't work, undefined
            alert("Did it work? (2) " + this.shoutIt) //works
    })();
    

    【讨论】:

      猜你喜欢
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-11
      • 2014-11-08
      相关资源
      最近更新 更多