this
在js中一切皆对象,所以我们可以给function添加额外的属性并且获取添加的属性。 那么我们如何访问通过prototype添加的方案呢?
答案是 this
我们知道每个function都会自动获得this,在这一点上,如果我们改变了函数的执行上下文,也就改变了this
this肯定被用在function中,并且永远指向一个对象,这个对象会替换掉function中引用的this
bind
bind返回一个新函数,新函数的this会被设置成给定的参数
var a = {
name: '233',
say: function(){
return this.name;
}
}
function b(arg1, arg2){
console.log(this.say(), arg1, arg2) // 在c函数中this被设置成了对象a
}
var c = b.bind(a, 2, 3) // 对象a替换掉了function b中用到this的地方
c(); // '233'
call,apply
call,apply返回用新的this调用函数的返回值
var a = {
name: '233',
say: function(){
return this.name;
}
}
function b(arg1, arg2){
console.log(this.say(), arg1, arg2) // 在c函数中this被设置成了对象a
}
b.call(a, 44, 55) // 233 44 55 // 对象a替换掉了function b中用到this的地方
b.apply(a, [44, 55]) // 233 44 55 // 对象a替换掉了function b中用到this的地方
思考题
有了call和apply为何需要bind?
其实从返回值可略知一二,call和apply是立即执行的,然而bind是返回的一个函数,想什么时候执行什么时候执行。这对event是很有用的。
应用
实现new
function newFunc(father, ...rest){
let son = {}
son.prototype = father.__proto__;
let son2 = father.apply(son, rest); //为何apply?将参数用到son身上!!
if(son2!==null && (typeof son2 === 'object' || typeof son2 === 'function')){
return son2;
}else{
return son;
}
}