小编use*_*668的帖子

为什么在函数内部创建同名变量时不会覆盖函数参数?

var a = 'why is this not undefined?';
function checkScope(a) {
    var a;
    console.log(a);
}
checkScope(a);
Run Code Online (Sandbox Code Playgroud)

Javascript是功能范围语言,对吗?当我在与函数参数使用相同名称的函数内部声明一个新变量时,为什么新定义的变量仍然保存与参数相同的数据?

我以为它应该是未定义的?

javascript variables function-parameter

21
推荐指数
1
解决办法
2743
查看次数

在javascript中使用'this'在自身内部使用相同对象的键值对

假设我有两个对象,例如

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 属性而第二个函数可以?我真的很困惑。

javascript object

5
推荐指数
2
解决办法
1471
查看次数