面试题201412——core 变量作用域

作者:zccst

1,var a = b = 5;

(function(){
var a = b = 5;
})();
console.log(a);//undefined 报错
console.log(b);//5 等价于 window.b = 5, var a = b;


2,变量和函数的声明提前

function test(){
console.log(a);//undefined
console.log(foo());//2
var a = 1 ;
function foo() { return 2;};
}
test();

原因:变量和函数的申明都被提前了,但是变量没有赋值,因此是undefined


3,作用域链

var fullName = "C";
var obj = {
fullName : "A",
prop:{
fullName:"B",
getFullName:function(){
return this.fullName;
}
}
}
console.log(obj.prop.getFullName());//B
var dd = obj.prop.getFullName;
console.log(dd());//C
console.log(dd.call(obj.prop));//B



4,对象及引用

a = 66;
var a = {"x": 1};
var b = a;
a.x = 2;
console.log(b.x);

a = {"x":3};
console.log(b.x);
a.x = 4;
console.log(b.x);

结果:全是2


如果您觉得本文的内容对您的学习有所帮助,您可以微信:
[img]http://dl2.iteye.com/upload/attachment/0109/0668/fb266dfa-95ca-3d09-b41e-5f04a19ba9a1.png[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值