new 操作符绑定this (bind)

  bind(a,b)//     bind方法中:a代表把this指向a的作用域     b代表参数   

---------------------------------------new 绑定代码

function foo(p1,p2){
  this.val=p1+p2;
}
var bar=foo.bind(null,"p1","p2");  //把函数foo的引用附给了bar  并把foo的this指向了null   给foo传进了参数p1  

var baz=new bar("p2");//   new操作符把foo函数的this指向了foo本身  并传进了参数p2
baz.val;//
bar是函数foo的应用-------function     
new bar----------------------obj

function foo(a,b){
console.log("a:"+a+",b:"+b);
};
foo.apply(null,[2,3]);.//a:2,b:3
var fun=foo.bind(null,6);------//把a变成了6
fun(7);//把b变成了7

原文地址:https://www.cnblogs.com/trend/p/7552059.html