我是javascript新手,为什么这段代码返回undefined?为什么这段代码返回undefined?
const foo = {
bar: function() { return this.baz; },
baz: 1,
};
console.log((function() {
return typeof arguments[0]();
})(foo.bar));
我是javascript新手,为什么这段代码返回undefined?为什么这段代码返回undefined?
const foo = {
bar: function() { return this.baz; },
baz: 1,
};
console.log((function() {
return typeof arguments[0]();
})(foo.bar));
如果您希望number
的回答不能从您的代码中返回。 这是对这个关键词的误解(正如@Tushar @Bergi指出的那样)
下面的代码可以做到这一点。
function Foo(){
this.baz = 1;
};
Foo.prototype.bar = function() {
"use strict";
return this.baz;
}
var foo = new Foo();
console.log((function() {
"use strict";
return typeof arguments[0];
})(foo.bar()));
你从哪里找到这段代码?那个IIFE和'arguments [0]'这个东西看起来不必要的复杂而且与你的问题无关。 – Bergi
当你执行参数[0]()'这个'是指它被调用的上下文,在这种情况下,它很可能是'window','window'将不会有一个名为'baz'的属性 – JohanP
@Tushar在那里?随意重新打开并回答,但我认为唯一的/主要的问题是['this'关键字]的误解(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) – Bergi