`
wyzxzws
  • 浏览: 378256 次
  • 性别: Icon_minigender_1
  • 来自: dazhou
社区版块
存档分类
最新评论

面试总结(二)-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 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics