简体   繁体   English

Javascript面向对象的应用程序结构

[英]Javascript object-oriented application structure

I am trying to rewrite a JS web-application in a more object-oriented way. 我试图以一种更加面向对象的方式重写JS Web应用程序。

Say I have a main object called Application: 假设我有一个名为Application的主要对象:

var Application = (function(obj) {

    obj.init = function() {
        console.log("Application init");
    };
    obj.move = function() {
        return {
            left: function() { console.log("Move left"); },
            right: function() { console.log("Move right"); }
        }
    }

    return obj;

})(Application || {});

I'd like to add another method which could have multiple instances, so I'll need to add a prototype: (This happens in another file) 我想添加另一个可能有多个实例的方法,所以我需要添加一个原型:(这发生在另一个文件中)

var Application = (function(obj) {

    obj.child = function() {
        return {
            typeA: function() {
                console.log("New child object of type A");
                this.childID = Math.floor((Math.random() * 10) + 1); // Just a random number
            },
            typeB: function() {
                console.log("New child object of type B");
                this.childID = Math.floor((Math.random() * 10) + 1); // Just a random number
            }
        }
        // This is where it goes wrong
        obj.child.typeA.prototype = function() {
            getID: function() { console.log(this.childID) }
        }
    };

    return obj;

})(Application || {});

I problem is that I cannot access the prototype function getID for typeA() . 我的问题是我无法访问typeA()的原型函数getID This is probably because of the way I have structured the application. 这可能是由于我构造应用程序的方式。 I'd like to be able to call the prototype method like so: 我希望能够像这样调用原型方法:

var a = Application.child.typeA();
a.getID();

How do I add a prototype for an object like this in a proper way? 如何以适当的方式为这样的对象添加原型?

I'm new to object-oriented Javascript and as there are so many ways to structure an object-oriented application in JS I pretty sure I am messing them up. 我是面向对象的Java的新手,因为有很多方法可以在JS中构建面向对象的应用程序,所以我很确定自己会弄乱它们。

What am I doing wrong here? 我在这里做错了什么?

child is a function that returns an object, you seem to want to make it the object itself. child是一个返回对象的函数,您似乎想使其成为对象本身。 Also, it did return before the prototype assignment could have been done. 而且,它确实在完成原型分配之前就return了。 And the prototype "object" itself was a syntactic mix between an object literal and a function. 原型“对象”本身就是对象文字和函数之间的语法混合。

Instead, use 相反,使用

var Application = (function(obj) {

    obj.child = {
        typeA: function() {
            console.log("New child object of type A");
            this.childID = Math.floor((Math.random() * 10) + 1); // Just a random number
        },
        typeB: function() {
            console.log("New child object of type B");
            this.childID = Math.floor((Math.random() * 10) + 1); // Just a random number
        }
    };
    obj.child.typeA.prototype.getID = function() {
         console.log(this.childID)
    };

    return obj;
})(Application || {});

You might also directly assign an object literal to .prototype , but that is not recommended over amending the existing object. 您也可以将对象文字直接分配给.prototype ,但是建议不要在修改现有对象时使用。

Is the way I am structuring my application a good practice? 我构建应用程序的方式是否是一种好习惯?

Yes, it's a common pattern. 是的,这是一种常见的模式。

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

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