js中关于this关键字的理解
2017-04-11 21:41
651 查看
this是函数运行时,自动生成的一个内部对象,只能在函数内部使用。随着函数使用场合的不同,this的值会发生变化。但是有一个总的原则,那就是this指的是,调用函数的那个对象。
1.this指代全局对象Global:
2.某个对象的方法调用时,this指上级对象:
3.构造函数调用时,指向新对象:
4.apply方法调用时,this指的是第一个参数:
apply()的参数为空时,默认调用全局对象.
1.this指代全局对象Global:
var x = 1; function test(){ console.log(this.x); } test(); // 1
2.某个对象的方法调用时,this指上级对象:
function test(){ console.log(this.x); } var o = {}; o.x = 1; o.m = test; o.m(); // 1
3.构造函数调用时,指向新对象:
var x = 2; function test(){ this.x = 1; } var o = new test(); console.log(o.x); //1 alert(x); //2
4.apply方法调用时,this指的是第一个参数:
var x = 0; function test(){ alert(this.x); } var o={}; o.x = 1; o.m = test; o.m.apply(); //0 o.m.apply(o); //1
apply()的参数为空时,默认调用全局对象.
相关文章推荐
- 关于javascript中this关键字 【翻译+自我理解】
- 关于javascript中this关键字(翻译+自我理解)
- 关于js 的 match函数的一点个人理解(jquery.form.js match)
- 关于JS中的this关键字
- 关于js中prototype的理解
- 关于js中类的静态属性和实例属性的理解
- 理解 backbone.js 中的 bind 和 bindAll 方法,关于如何在方法中指定其中的
- 关于js闭包的理解
- 关于js异步加载的理解
- 关于js中单引号(')和双引号(")的使用以及转义的理解
- (重点)(关于JS调用的方式)JS 传值传值传值传值传值 没想到的失误,知识的不理解
- 关于js中this 和 prototype的 理解。
- 关于 js 的 prototype , call,apply ,闭包,回调 个人浅显理解
- 关于js中单引号(')和双引号(")的使用以及转义的理解
- JS 关于(function( window, undefined ) {})(window)写法的理解
- 关于javascript中this关键字(翻译+自我理解)
- 关于js中的this关键字
- 关于js中单引号(')和双引号(")的使用以及转义的理解
- 理解 backbone.js 中的 bind 和 bindAll 方法,关于如何在方法中指定其中的 this,包含apply方法的说明[转载]
- js关于原型构造函数和原型链的理解