【问题标题】:JavaScript class prototyping using module pattern使用模块模式的 JavaScript 类原型
【发布时间】:2012-08-16 11:01:05
【问题描述】:

我正在寻找在 JavaScript 中定义类的方法。我想出了混合模块和原型模式,但不确定我是否错过了什么。基本上我想使用'this'关键字。示例:

var A = function()
    {
    this.x = 10;
    };

A.prototype = (function()
    {
    function privatePrint()
        {
        alert("Printing from private! x:" + this.x);
        }
    this.print = function()
        {
        privatePrint.call(this);
        };
    return this;
    }).apply(A.prototype);

var a = new A();
a.print();

返回值只是为了可读性 - A.prototype 可以在开头使用。

我也尝试过的模式:

  • 模块:不能使用“新”关键字。
  • 原型,展示原型: 如果在原型声明中声明了私有函数,则没有扩展 (对象返回的公共方法)

我的方法可以接受吗?

【问题讨论】:

  • 我看不出这种方法有什么用...有什么好处?这似乎令人困惑。为什么不只是A.prototype = { ... }..
  • 设置prototype到和object后,不能用同样的操作来扩展它。

标签: javascript design-patterns module this prototype


【解决方案1】:
**Public**

function Constructor(...) {
   this.membername = value;
}
Constructor.prototype.membername = value;

**Private**

function Constructor(...) {
   var that = this;
   var membername = value;
   function membername(...) {...}

}

Note: The function statement

function membername(...) {...}

is shorthand for

var membername = function membername(...) {...};

**Privileged**

function Constructor(...) {
   this.membername = function (...) {...};
}

【讨论】:

【解决方案2】:

距您提出要求已经两年多了,但在谷歌搜索类似的方法时,我最终来到了这里。除了(因为您本质上是在征求意见)之外,我没有看到您的实现有任何缺点,为什么您将原型作为导入传递给 IIFE 似乎有点令人困惑。

否则,您所得到的看起来与我所看到的 “显示原型模式” 的其他“标准”实现非常相似:

(function (NS) {

    'use strict';

    // constructor for the Person "Class", attached to your global namespace
    var Person = NS.Person = function (name) {
        // set properties unique for each instance
        this.name = name;
    };

    // may not be necessary, but safe
    Person.prototype.constructor = Person;

    // private method
    var _privateMethod = function() {
        // do private stuff
        // use the "_" convention to mark as private
        // this is scoped to the modules' IIFE wrapper, but not bound the returned "Person" object, i.e. it is private
    };

    // public method
    Person.prototype.speak = function() {
        console.log("Hello there, I'm " + this.name);
    };

    return Person;

})(window.NS = window.NS || {}); // import a global namespace

// use your namespaced Person "Class"
var david = new NS.Person("David");
david.speak();

还有一个类似的模块模式,其结构可能更像是您所追求的“类”实现

(function (NS) {

    'use strict';

    // constructor for the Person "Class", attached to your global namespace
    var Person = NS.Person = function (name) {

        // reset constructor (the prototype is completely overwritten below)
        this.constructor = Person;

        // set properties unique for each instance
        this.name = name;
    };

    // all methods on the prototype
    Person.prototype = (function() {

        // private method
        var _privateMethod = function() {
            // do private stuff
            // use the "_" convention to mark as private
            // this is scoped to the IIFE but not bound to the returned object, i.e. it is private
        };

        // public method
        var speak = function() {
            console.log("Hello there, I'm " + this.name);
        };

        // returned object with public methods
        return {
            speak: speak
        };
    }());

})(window.NS = window.NS || {}); // import a global namespace

// use your namespaced Person "Class"
var david = new NS.Person("David");
david.speak();

要点:https://gist.github.com/dgowrie/24fb3483051579b89512

【讨论】:

    猜你喜欢
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    相关资源
    最近更新 更多