0

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 を使用できます。

私も試したパターン:

  • モジュール : 'new' キーワードは使用できません。
  • プロトタイプ、プロトタイプの公開 : プロトタイプ宣言でプライベート関数が宣言されている場合は拡張なし (オブジェクトによって返されるパブリック メソッド)

私のアプローチは受け入れられますか?

4

2 に答える 2

0

あなたが尋ねてから2年以上経ちましたが、グーグルで同様のアプローチを求めてここにたどり着きました. (基本的に意見を求めているため)IIFEのインポートとしてプロトタイプを渡す理由が少し混乱しているように見える以外に、実装に欠点はありません。

それ以外の場合、あなたが持っているものは、私が本質的にそのように見た「Revealing Prototype Pattern」の他の「標準」実装と非常によく似ています:

(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

于 2015-01-31T05:30:25.457 に答える
0
**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 (...) {...};
}
于 2012-08-16T11:07:21.543 に答える