call和apply的区别:
call和apply都能改变this的指向
call传实际参数,apply传arguments集合
call和apply的应用场景:用别人的构造函数来创建自己的构造函数
<script>
function GetColor(color){
this.color = color;
}
function GetHeight(height){
this.height = height;
}
function GetWidth(width){
this.width = width;
}
function Box(color,height,width,style){
this.style = style;
// call的使用
GetColor.call(this,color);
GetHeight.call(this,height);
GetWidth.call(this,width);
//apply的使用
// GetColor.apply(this,[color]);
// GetHeight.apply(this,[height]);
// GetWidth.apply(this,[width]);
}
var box = new Box('red',100,100,'float');
console.log(box);
</script>
打印结果