javascript--call--apply

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方法,可以把该对象的该方法作用于另一个对象。

posted on 2013-03-02 12:10  mtima  阅读(143)  评论(0编辑  收藏  举报

导航