【发布时间】:2020-04-01 01:11:33
【问题描述】:
我的代码如下:
function t() {
var name = "abc";
bar = function() {
console.dir(this);
console.log('bar');
};
};
t.foo = function() {
console.dir(this);
this();
console.log('bar');
};
console.dir(t);
它给出如下输出:
ƒ t()
foo: ƒ ()
arguments: null
caller: null
length: 0
name: "t"
prototype: {constructor: ƒ}
__proto__: ƒ ()
[[FunctionLocation]]: VM2091:1
[[Scopes]]: Scopes[2]
所以我们可以看到,在检查函数 t() 时,我们没有找到函数 "bar",但函数 "foo" 在函数 t() 中。我的问题是为什么函数 "bar" 不是函数 t() 的属性,而函数“foo”成为函数 t() 的属性?
【问题讨论】:
标签: javascript javascript-objects prototypejs