1.new Parent.num代码相当于new (Parent.num);new Parent().num相当于(new Parent()).num。由此看来 new的构造函数后跟括号优先级会提升。
2.call
、apply
、bind
方法的共同点和区别:apply
、 call
、bind
三者都是用来改变函数的this对象的指向的;apply
、 call
、bind
三者第一个参数都是this要指向的对象,也就是想指定的上下文(函数的每次调用都会拥有一个特殊值——本次调用的上下文(context)——这就是this
关键字的值。)
bind
是返回对应函数,便于稍后调用;apply
、call
则是立即调用 。
function eat(){
console.log("eat");
}
function drink(){
console.log("drink");
}
drink.call(eat); //eat
function a(x,y){
console.log(x+y);
this(3,2);
}
var c= function (x,y) {
console.log(x-y)
};
a.call(c,3,2); // 5,1
function Animal(){
this.name="animal";
this.showName=function(){
console.log(this.name);
}
}
function Dog(){
this.name="dog";
}
var animal=new Animal();
var dog=new Dog();
animal.showName.call(dog); // dog
class1.call(this,args1,args2);
/*或*/
class1.apply(this,[args1,args2]);
3.