JavaScript 中的This

知乎中看到This的解释最好的就是当成Call函数来看

比如Object.func()中的this 就是Object.func.call(Object)

func() 中的this 就是func.call(undefined)

func(a,b)中的this 就是func.call(undefined,a,b)

O.a.b.c()中的this 就是O.a.b.c.call(O.a.b)

但在具体的调用中会经常使用到的2中情况

1. 多层调用

这时可以用 var that = this;

2. foreach中的多层调用

这时可以用foreach的回调函数中的第二个参数来解决

 
forEach.call(element.childNodes, function(child){
  child.parentNode.style.color = '#0F0';
});

 

to be continued....

posted on 2016-12-06 15:34  GoldenRazor  阅读(135)  评论(0编辑  收藏  举报

导航