《你不知道的javascript上卷》读书笔记(kyle simpson 著,赵望野、梁译)

一、关于this篇:

1.this实际上是在函数被调用时发生的绑定,它指向什么完全取决于函数在哪里被调用

2.this优先级:new>显示绑定>隐式绑定>默认绑定。

3.如果把null或undefined作为this的绑定对象传入call、apply或者bind,这些值在调用时会被忽略,实际应用的是默认绑定规则

eg:

function foo(){
  console.log(this.a);
}

var a=2;

foo,bind(null);  //2

4.将arguments(类数组)变为数组

(1)Array.prototype.slice.apply(arguments)     或   [].slice.apply(arguments)  或  new Array().slice.apply(arguments)

(2)es6扩展运算符...

function foo(...arg){
console.log(arg);
}


foo.apply(null,[155,2,3,4,56,7]);   或    foo(155,2,3,4,56,7)

猜你喜欢

转载自www.cnblogs.com/miaSlady/p/9576455.html