声明具有类似属性的JavaScript对象的最佳实践

时间:2023-02-06 21:15:39

For a client side JS application, I require creating Quizzes and Surveys. The domain logic for these objects is incredibly similar so I would like them to both inherit from one UserInput object, while they each have very few additional methods each.

对于客户端JS应用程序,我需要创建测验和调查。这些对象的域逻辑非常相似,所以我希望它们都从一个UserInput对象继承,而每个对象都有很少的附加方法。

I'm concerned with the best practice method of doing this. I was thinking right off the top of my head I can just create a UserInput constructor that acts as a prototype, and then dynamically add methods to both Quizzes and Surveys.

我关心这样做的最佳实践方法。我正在思考我的头脑,我可以创建一个充当原型的UserInput构造函数,然后动态地向Quizzes和Surveys添加方法。

For instance:

function UserInput() {}
UserObject.prototype.sharedMethod = function() {}

Quiz = new UserInput();
Quiz.sharedMethod()
Quiz.quizMethod = function() {}

Survey = new UserInput();
Survey.surveyMethod = function() {}

Survey.sharedMethod()

However I wonder if this is just not plain monkey-patching, and implementing a similar idea to abstract classes in JavaScript would be cleaner.

但是我想知道这不是简单的猴子修补,并且在JavaScript中实现类似的抽象类的想法会更清晰。

var Quiz = function() {
 UserInput.apply(this, arguments)
}
Quiz.prototype = UserInput();
Quiz.prototype.constructor = quiz;
Quiz.prototype.quizMethod = function() {}

The immediate difference I see is in the first example quiz/survey are objects while in the second quiz is a prototype. Every page has a singleton of these items, so having a method on every instance of quiz (which is only one) does not seem that bad to me.

我看到的直接差异是在第一个例子中测验/调查是对象,而在第二个测验中是原型。每个页面都有这些项目的单例,因此在每个测验实例(只有一个)上有一个方法对我来说似乎并不坏。

Which is preferred and why? (Or am I missing any better methods of instantiating objects).

哪个是首选,为什么? (或者我错过了任何更好的实例化对象的方法)。

1 个解决方案

#1


0  

The one I use is available on github and it works well for me with a simple familiar sytax. Allows OOP concepts in javascript including classes, inheritance, multi-inheritance, polymophism, interfaces (code contracts), and enumerators

我使用的那个可以在github上使用,它对我来说非常适合使用简单熟悉的sytax。允许在javascript中使用OOP概念,包括类,继承,多继承,多态,接口(代码契约)和枚举器

Heres an example of simple class inheritance:

下面是简单类继承的一个例子:

/* using ds.oop.min.js */

var a= ds.class({
    type: 'a',
    constructor: function (x) { this.x = x; },
    mul: function (s) {
        this.x *= s;
        return this;
    }
});
var b= ds.class({
    type: 'b',
    inherits: a,              
    constructor: function (x) { this.x = x; },
    sub: function (s) {
        this.x -= s;
        return this;
    }
});
var o = new b(5);
var output = o.mul(3).sub(5);    // output = 10

#1


0  

The one I use is available on github and it works well for me with a simple familiar sytax. Allows OOP concepts in javascript including classes, inheritance, multi-inheritance, polymophism, interfaces (code contracts), and enumerators

我使用的那个可以在github上使用,它对我来说非常适合使用简单熟悉的sytax。允许在javascript中使用OOP概念,包括类,继承,多继承,多态,接口(代码契约)和枚举器

Heres an example of simple class inheritance:

下面是简单类继承的一个例子:

/* using ds.oop.min.js */

var a= ds.class({
    type: 'a',
    constructor: function (x) { this.x = x; },
    mul: function (s) {
        this.x *= s;
        return this;
    }
});
var b= ds.class({
    type: 'b',
    inherits: a,              
    constructor: function (x) { this.x = x; },
    sub: function (s) {
        this.x -= s;
        return this;
    }
});
var o = new b(5);
var output = o.mul(3).sub(5);    // output = 10