Javascript 为什么我需要使用;这";访问匿名自执行函数的参数时的关键字?
我刚刚开始学习Javascript,我一直在玩匿名自动执行函数。我编写了一些代码,但没有按照我预期的方式工作。为什么在这个实例中需要“this”关键字来获取变量“shoutIt”的值 第一个警报显示“工作正常吗?(1)未定义”,而第二个警报显示“工作正常吗?(2)[是!]” 谢谢Javascript 为什么我需要使用;这";访问匿名自执行函数的参数时的关键字?,javascript,anonymous-function,Javascript,Anonymous Function,我刚刚开始学习Javascript,我一直在玩匿名自动执行函数。我编写了一些代码,但没有按照我预期的方式工作。为什么在这个实例中需要“this”关键字来获取变量“shoutIt”的值 第一个警报显示“工作正常吗?(1)未定义”,而第二个警报显示“工作正常吗?(2)[是!]” 谢谢 var shoutIt = "[YES!]"; //creating an anonymous, self-executing function ( function (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
作为参数,但您没有向它传递任何内容
基本上,您的函数需要一个参数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
在全局级别定义的任何内容都会自动链接到窗口对象。因为匿名函数希望将
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
在全局级别定义的任何变量都会自动链接到窗口对象。这里有两个变量称为
shoutIt
:一个是由var shoutIt
定义的全局变量,另一个是由函数(shoutIt){…
中的形式参数定义的变量
当您在非严格模式下运行非方法函数(即形式为foo()
而不是bar.foo()
)时,此
等于全局对象(在浏览器中,窗口
)。在函数内部,此.shoutIt
指的是全局范围内的shoutIt
变量
相比之下,shoutIt
在这里引用的是具有该名称的函数参数,而不是向上一级作用域的全局变量(全局变量被同名的更直接的变量“隐藏”)函数没有使用任何参数调用,因此函数中的shoutIt
值是undefined
如果要传入一个值作为名为shoutIt
的参数,请在调用括号中提供一个值:
(function (shoutIt) {
...
})(someValue);
这里有两个变量称为
shoutIt
:一个是由var shoutIt
定义的全局变量,另一个是由函数(shoutIt){…
当您在非严格模式下运行非方法函数(即形式为foo()
而不是bar.foo()
)时,此
等于全局对象(在浏览器中,窗口
)。在函数内部,此.shoutIt
指的是全局范围内的shoutIt
变量
相比之下,shoutIt
在这里引用的是具有该名称的函数参数,而不是向上一级作用域的全局变量(全局变量被同名的更直接的变量“隐藏”)函数没有使用任何参数调用,因此函数中的shoutIt
值是undefined
如果要传入一个值作为名为shoutIt
的参数,请在调用括号中提供一个值:
(function (shoutIt) {
...
})(someValue);
您的参数与函数外部的变量命名相同,并且您没有将变量传递到函数中 有了你的例子,你可以做一些不同的事情,让它按照你的预期工作 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
})();
您的参数与函数外部的变量命名相同,并且您没有将变量传递到函数中 有了你的例子,你可以做一些不同的事情,让它按照你的预期工作 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
})();
在您的代码中,
this
不是函数,this
是全局作用域。putconsole.log(this)
在您的函数中,检查输出。您应该使用严格模式,否则它将无法按预期工作。我不知道有不同的模式。我将启用严格模式,谢谢。在您的代码中,此
不是函数,此
是全局范围。放置控制台.log(此)
在你的函数中,检查输出。你应该使用严格模式,那么它就不会像预期的那样工作。我不知道有不同的模式。我会打开严格模式,谢谢。谢谢,这很有意义,因为我现在没有像我想的那样传递参数。不确定如何选择这个作为我的答案,但它帮了我很多忙!:)很高兴我我会帮助@Austingerschlert谢谢你,现在我没有像以前那样传递参数,这是有道理的