JS中call()和apply()

每個函數都包含兩個非繼承而來的方法:call()和apply();

在JavaScript中,call和apply作用是一樣的,都是爲了改變某個函數運行時的上下文(context)而存在的,換句話說,就是爲了改變函數體內部this的指向。

function fruits(){}
        
fruits.prototype = {
    color: "red",
    say: function(){
        console.log("My color is " + this.color);
    }
};

var apple = new fruits;
apple.say();                //My color is red

當想另外一個對象想使用fruits中的say方法時不用重新寫,使用call和apply可以實現“劫持”別人的方法。

function fruits(){}
            
fruits.prototype = {
    color: "red",
    say: function(){
        console.log("My color is " + this.color);
    }
};

var another = {
    color: "yellow"
};

var apple = new fruits;
apple.say();                //My color is red
apple.say.call(another);    //My color is yellow
apple.say.apply(another);   //My color is yellow

區別:參數書寫方式不同

call(thisObj, arg1, arg2, arg3, arg4);
apply(thisObj, [args]);

thisObj:call和apply第一個參數是一樣的,該參數將替代Function類裏面的this對象。
arg1,arg2....:是一個個的參數,
args:一個數組或類數組,是一個參數列表。

用法

var name = "小白";
var obj = {
    name: "小紅"
};

function sayName() {
    return this.name;
}
console.log(sayName.call(this));   //小白
console.log(sayName.call(obj));    //小紅

實現繼承

//實現js繼承
//父類
function Person(name, height) {
    this.sayInfo = function() {
        return "姓名:" + name + ", 身高:" + height + ", 體重:" + this.weight;
    }
}
//子類
function Chinese(name, height, weight) {
    Person.call(this, name, height);
    this.weight = weight;
    
    this.nation = function() {
        console.log("我是中國人");
    }
}
//子類
function America(name, height, weight) {
    Person.call(this, name, height); //America 繼承了 Person的方法,簡單的說,就是把Person的方法全放到了 America 
    this.weight = weight;
}

let chiness = new Chinese("成龍", "178cm", "60kg");
console.log(chiness.sayInfo());    //姓名:成龍, 身高:178cm, 體重:60kg
let america = new America("jack", "180cm", "55kg");
console.log(america.sayInfo());    //姓名:jack, 身高:180cm, 體重:55kg
本篇文章是借鑑的,因爲寫的不錯,冒犯之處,請見諒
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章