与Prototypes链接?用于命名空间目的

时间:2015-05-13 15:46:00

标签: javascript

说我有echo json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

Dog

我想在其原型上命名一些函数。

function Dog(name) {
  this.name = name;
}

所以我可以这样称呼它:

Dog.prototype.movement = {
  this.run = this.name + 'is running',
  this.jump = function() { console.log(this.name + 'is jummping');}
}

1 个答案:

答案 0 :(得分:3)

你很接近,只需将runjump视为对象属性而不是变量赋值:

Dog.prototype = {
  movement: function() {
    return {
      run: function() { 
        this.name + 'is running' 
      }.bind(this),
      jump: function() { 
        console.log(this.name + ' is jummping');
      }.bind(this)
    }
  }
}

var carl = new Dog('Car');
carl.movement().jump();