Javascript命名空间声明与函数原型 [英] Javascript namespace declaration with function-prototype

查看:86
本文介绍了Javascript命名空间声明与函数原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,这是经常讨论的。但是,在寻找像19世纪的人之后,我需要一些建议。我没有问题,通过声明一个命名空间,但是当涉及到一个prototype.foo函数,我坚持。我找到了一种方法,但我不喜欢它:

I know, this is often discussed. But after searching around like someone out of the 19th century, I need some advice. I have no problem by declaring a "namespace", but when it comes to a prototype.foo function, I stuck. I found a way, but I don't like it:

Namespace = {}
Namespace.obj = function() {
    this.foo="bar";
}
Namespace.obj.prototype.start = function() {
    this.foo="fubar";
}

blah = new Namespace.obj();
blah.start();

现在,由于我有点神经质的脚本,我想有一些像这个:

Now, since I'm a little neurotic in case of scripting, I would like to have something like this:

Namespace = {
    obj: function() {
        this.foo="bar";
    },
    obj.prototype.start: function(tabinst) {
        this.foo="fubar";
    }
}
...

错误:
未捕获的语法错误:意外的令牌。

But then it throws an error: "Uncaught SyntaxError: Unexpected token ."

我知道,这是化妆,但我认为有一个更好的方法声明包含类和原型函数的命名空间。

I know, this is cosmetic, but I think that there has to be a better method of declaring a "namespace" containing a class and prototype functions.

推荐答案

我会这样做的方法是使用模块模式

您基本上将所有模块逻辑封装在一个自我执行的函数,它会返回一个具有你的类,函数,变量等的对象。想象一下暴露你的Module API的返回值。

The way I would do it is using the "Module pattern".
You basically encapsulate all your "Module" logic in a self executing function that would return an object having your classes, functions, variables etc... Think of the return value as exposing your Module API.

Namespace = (function () {
    /** Class obj **/
    var obj = function () {
        this.foo = 'bar';
    };
    obj.prototype = {
        start: function () {
            this.foo = 'fubar';
        }
    };

    /** Class obj2 **/  
    var obj2 = function () {
        this.bar = 'foo'
    };
    obj2.prototype = {
        start: function () {
            this.bar = 'barfoo';
        },
        end: function () {
            this.bar = '';
        }
    };
    return {
        obj : obj,
        obj2: obj2
    };
})();

var o = new Namespace.obj()
o.start()

为了进一步封装obj类方法和构造函数,我们可以执行以下操作:

In order to further encapsulate the "obj" class methods and constructor we could do the following:

/** Class obj **/
var obj = (function () {
    /** class Constructor **/
    var obj = function () {
        this.foo = 'bar';
    };
    /** class methods **/
    obj.prototype = {
        start: function () {
            this.foo = 'fubar';
        }
    };
    return obj;
})();

还有一个重要的功能是免费使用这种模式,即私有变量请考虑以下内容:

There is also an important feature that comes for free using this pattern, which is "Private variables", consider the following:

/** Class Foo **/
var Foo = (function () {
    // Private variables
    var private_number = 200
    /** class Constructor **/
    var Foo = function () {
        this.bar = 0;
    };
    /** class methods **/
    Foo.prototype = {
        add: function () {
            this.bar += private_number;
        }
    };
    return Foo;
})();

foo = new Foo();
alert(foo.bar); // 0
foo.add(); 
alert(foo.bar);// 200
alert(foo.private_number) //undefined

这篇关于Javascript命名空间声明与函数原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆