var a = 'why is this not undefined?';
function checkScope(a) {
var a;
console.log(a);
}
checkScope(a);
Run Code Online (Sandbox Code Playgroud)
Javascript是功能范围语言,对吗?当我在与函数参数使用相同名称的函数内部声明一个新变量时,为什么新定义的变量仍然保存与参数相同的数据?
我以为它应该是未定义的?
假设我有两个对象,例如
var a = {
b: 1,
c: this.b
};
Run Code Online (Sandbox Code Playgroud)
和
var funcObj = {
b : function() {
return 1;
},
c: function() {
console.log(return this.b())
}
}
Run Code Online (Sandbox Code Playgroud)
在记录这两个像
console.log(a.c)//results undefined
console.log(funcObj.c()) //results 1
Run Code Online (Sandbox Code Playgroud)
为什么第一个函数不能使用 this 属性而第二个函数可以?我真的很困惑。