面试总结(二)-javascript中this用法积累

最近面试中,javascript中的this用法,自己测试结果如下
测试环境:360快速浏览器,firefox

var a = 2;
var obj = {
	a : 1,
	getA : function(){
		return this.a;
	}
};

console.log(window.a + "+++"+a+"---result ==== "+obj.getA());//2+++2---result ==== 1 


b = 2;
var obj2 = {
	b : 1,
	getB : function(){
		return this.b;
	}
};

console.log(window.b + "+++"+b+"--result ==== "+obj2.getB());//2+++2--result ==== 1 

(function(param){
	param = param || {};
	var obj_a = param.A, obj_b = param.B;
	console.log(window.a + "+++"+a+"--result ==== "+obj_a.getA());//2+++2--result ==== 1 
	console.log(window.b + "+++"+b+"--result ==== "+obj_b.getB());//2+++2--result ==== 1 
})({A:obj,B:obj2});

(function(param){
	param = param || {};
	b = 3;
	a = 4;
	var obj_a = param.A, obj_b = param.B;
	console.log(window.a + "+++"+a+"--result ==== "+obj_a.getA());//4+++4--result ==== 1  
	console.log(window.b + "+++"+b+"--result ==== "+obj_b.getB());//3+++3--result ==== 1 
})({A:obj,B:obj2});

(function(param){
	param = param || {};
	var b = 3, a = 4;
	var obj_a = param.A, obj_b = param.B;
	console.log(window.a + "+++"+a+"--result ==== "+obj_a.getA());//4+++4--result ==== 1 
	console.log(window.b + "+++"+b+"--result ==== "+obj_b.getB());//3+++3--result ==== 1 
})({A:obj,B:obj2});

console.log("函数声明中this用法///");
function FunTest(name){
	this.name = name;
}
FunTest.age = 18;
FunTest.getStaticAge = function(){
	return this.age;
}
FunTest.prototype.getName = function(){
	return this.name;
}
console.log("--result ==== "+new FunTest("Tom").getName());//--result ==== Tom 
console.log("--result ==== "+FunTest.getStaticAge());//--result ==== 18 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值