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.apply(this, [name, height]);
    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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM