JavaScript call

function dog() {
    this.sound = "wangwang~";
    this.shout = function() {
        alert(this.sound);
    }
}
function cat() {
    this.sound = "miao";
    this.shout = function() {
        alert(this.sound);
    }
}
var dd = new dog();
dd.shout.call(new cat(), ","); // 将dd的call方法交给cat对象去执行,即使cat对象没有此方法的情况下,dd.shout.call依然能起作用,因为这相当于赋予了cat
// shout的方法,并且执行时的上下文也变成了cat的上下文
原文地址:https://www.cnblogs.com/heben/p/5387360.html