简体   繁体   English

原型继承和静态方法

[英]Prototypal Inheritance and static methods

I'm trying to get used to the "real" prototypal inheritance of JavaScript (ECMAScript 5) but somehow my mind seems to be stuck in the classical inheritance pattern. 我正在尝试习惯JavaScript的“真正的”原型继承(ECMAScript 5),但不知怎的,我的思想似乎陷入了经典的继承模式。

I'd like to create a Vector object which performs simple operations such as adding, subtracting etc. 我想创建一个Vector对象,它执行简单的操作,如添加,减去等。

Now there are two scenarios: 现在有两种情况:

  • #1: Adding Vector B "to" Vector A (Vector A gets modified) #1:将矢量B“添加到”矢量A(矢量A被修改)
  • #2: Adding Vector B "to" Vector B (a new Vector C is created, which is the sum of A and B) #2:将矢量B“添加到”矢量B(创建一个新的矢量C,它是A和B的总和)

In classical inheritance I'd create an instance method for scenario #1 and a static method for case #2 but it seems like there are no static functions in prototypal inheritance. 在经典继承中,我将为场景#1创建一个实例方法,为场景#2创建静态方法,但似乎在原型继承中没有静态函数。

So, what's a clean way to realize these two scenarios? 那么,实现这两种情况的干净方法是什么?

Here's what I've got so far: 这是我到目前为止所得到的:

var Vector = {

  x: 0,
  y: 0,
  z: 0,

  initialize: function(x,y,z) {
    this.x = x;
    this.y = y;
    this.z = z;
    return this;
  },

  add: function(vec) {
    this.x += vec.x;
    this.y += vec.y;
    this.z += vec.z;
    return this;  
  },

  print: function() {
    console.log('x:', this.x, 'y:', this.y, 'z:', this.z);
  }
};

var firstVector = Object.create(Vector).initialize(1,2,3);
var secondVector =  Object.create(Vector).initialize(4,5,6);

firstVector.print();  // Outputs: x:1, y:2, z:3
secondVector.print(); // Outputs: x:4, y:5, z:6

firstVector.add(secondVector); 
firstVector.print(); // Outputs: x:5,y:7,z:9


// What I'm looking for:

var thirdVector = Vector.add(firstVector, secondVector);

Thanks for any advice! 谢谢你的建议!

Update : 更新

Here is my attempt to implement a static function using Paul's advice (thanks!): 这是我尝试使用Paul的建议实现静态函数(谢谢!):

var vectorPrototype = {
  hello: function() { console.log('hello I am the prototype'); }
};

var Vector = Object.create(vectorPrototype);

Vector.hello = function() { console.log('hello I am the static function'); };

Vector.init = function() {
  return Object.create(vectorPrototype);
}

var vec1 = Vector.init();

vec1.hello(); // says: 'hello I am the prototype'
Vector.hello(); // says: 'hello I am the static function'

Your Vector object is really just the prototype. Vector对象实际上就是原型。 You can use it with the Object.create function to create your Vector base/sub class. 您可以将它与Object.create函数一起使用来创建Vector基础/子类。 Then stick your static properties on your newly created Vector class. 然后将静态属性粘贴到新创建的Vector类上。 See here: http://jsfiddle.net/agYNc/1/ 见这里: http//jsfiddle.net/agYNc/1/

var vectorPrototype = {

  x: 0,
  y: 0,
  z: 0,

  initialize: function(x,y,z) {
    this.x = x;
    this.y = y;
    this.z = z;
    return this;
  },

  add: function(vec) {
    this.x += vec.x;
    this.y += vec.y;
    this.z += vec.z;
    return this;  
  },

  print: function() {
    console.log('x:', this.x, 'y:', this.y, 'z:', this.z);
  }
};

//create your base Vector type
var Vector = Object.create( vectorPrototype );


//Your static functions here
Vector.staticFunction = function ( vec1, vec2 ) {};


var firstVector = Object.create(Vector).initialize(1,2,3);
var secondVector =  Object.create(Vector).initialize(4,5,6);

firstVector.print();  // Outputs: x:1, y:2, z:3
secondVector.print(); // Outputs: x:4, y:5, z:6

firstVector.add(secondVector); 
firstVector.print(); // Outputs: x:5,y:7,z:9​

Here is a good example of using Object.create with inheritance, static and instance properties. 这是使用Object.create和继承,静态和实例属性的一个很好的例子。 https://github.com/webadvanced/takeCommand/blob/master/src/takeCommand.module.js https://github.com/webadvanced/takeCommand/blob/master/src/takeCommand.module.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM