javascriptcallapply

function A() {
  this.message = '';
}

function B() {
  var message = '';
  this.setMessage = function(msg) {
    this.message = msg;
  };
  this.getMessage = function() {
    return this.message;
  };
}

var b = new B();
var a = new A();
b.setMessage('hello panda');

b.setMessage.call(a, 'hello tiger!');
console.log(a.message);

javascript中使用一个对象的方法的call或appley方法,可以把该对象的该方法作用于另一个对象。

原文地址:https://www.cnblogs.com/mtima/p/2939901.html