[js] this
2016-08-31 15:59
127 查看
mdn
my git
this由调用方法与位置决定,与定义位置,作用域,上下文均无关
严格模式会绑定到undefined
通过call与apply绑定后的函数无法重新绑定(硬绑定)
绑定到外层的this,之后无法通过硬绑定重新绑定
严格意义上方法f不属于对象,只是由对象调用,从而将其this绑定到对象,因此可能会发生丢失
调用过程参考
var NewObj = function() {}
var Obj = new NewObj(); // this == Obj
函数不是对象的成员,只是一个引用,因此用对象赋值会使用默认绑定
a014
my git
this由调用方法与位置决定,与定义位置,作用域,上下文均无关
全局
绑定到全局对象this // window console.log(this) // window
函数调用
简单调用
绑定到全局function f() { console.log(this); } f(); // window
严格模式会绑定到undefined
// strict mode下绑定 function f() { 'use strict'; // 只有在调用的运行中使用strict mode才会绑定到undefined console.log(this.a); } var a = 2; (function () { f(); })(); // TypeError 'use strict' function f() { console.log(this.a); } var a = 2; (function () { 'use strict' f(); })(); // 2
使用apply,call,bind
是一种显式绑定function f() { console.log(this); } f.apply(obj); // obj f.call(obj); // obj var f1 = f.bind(obj); // f1 is a function which is always bind to obj f1(); // obj
通过call与apply绑定后的函数无法重新绑定(硬绑定)
function f() { console.log(this.a); } var obj = { a: 1 }; function f1() { f.call(obj); } var a = 2; f1.call(window); // 1 var f2 = f1.bind(obj); f2 = f1.bind(window); f2(); // 1
箭头表达式
无自己的this,参考绑定到外层的this,之后无法通过硬绑定重新绑定
var obj = { a: function () { return (() => console.log(this)); } } obj.a()(); // obj var f = obj.a(); f(); // obj 在obj的调用过程中外层this绑定了! var f2 = obj.a; f2()(); // window
对象调用
var obj = { func: function f() { console.log(this) } } obj.func() // obj
严格意义上方法f不属于对象,只是由对象调用,从而将其this绑定到对象,因此可能会发生丢失
(obj.func)() // obj var c; (c = obj.func)() // window (obj.func = obj.func)() // window // callback var obj = { f() { console.log(this); } } function f(fn) { fn(); // same as (fn = obj.f)() } f(obj.f); // windows
构造调用
this指向返回到新对象调用过程参考
var NewObj = function() {}
var Obj = new NewObj(); // this == Obj
优先级
new(构造调用) > 硬绑定(call,apply,bind) > 隐绑定(对象调用) > 默认(window)例外
硬绑定中传入null或undefined会应用默认绑定function f() { console.log(this) } var f1 = f.bind(null); var f2 = f.bind(); f1() // window f2() // window f.apply() // window f.call(null) // window
函数不是对象的成员,只是一个引用,因此用对象赋值会使用默认绑定
其他
map
map可将this传递到函数中,若不传递,默认为windowvar obj = { array: [1], f() { this.array.map(function (item) { console.log(this); }); }, f1() { this.array.map(function (item) { console.log(this); },this); } }; obj.f(); // window obj.f1(); // obj
a014
相关文章推荐
- js中的this
- 使用 this 指针和 prototype 实现 js 的 OO 时的一个区别
- js中with、this的用法
- 让您对js的 function, javascript内置对象,this概念及之间的关系不再迷惑
- js 中 this的探讨
- js 通过 this 传 id
- js中with、this的用法
- Js-----var x, this.x, x....inheritance
- ecshop compare.js this.data undefined
- 使用 this 指针和 prototype 实现 js 的 OO 时的一个区别
- JS var self =this;
- 学习JavaScript 的必备 (一),让您对js的 function, javascript内置对象,this概念及之间的关系不再迷惑。(希望能置为推荐篇,为更多的js初学者关注)
- 对js的 function.javascript内置对象,this概念及之间的关系不再迷惑
- js中with、this的用法
- js 的THIS对象
- *关于Js中this值得注意的一个地方
- js中this的总结
- js-----关于this
- 原来js中的this这么好用
- js中this的总结