2012-08-16 96 views
0

我正在尋找在JavaScript中定義類的方法。我想出了混合模塊和原型模式,但不知道如果我不想錯過什麼。基本上我想用'this'關鍵字。例如:使用模塊模式的JavaScript類原型設計

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(); 

返回值只是爲了便於閱讀 - 答:原型可以在開頭使用。

模式我也試過:

  • 模塊: '新' 關鍵字不能使用。
  • 原型,揭示原型: 沒有延伸如果原型聲明 (由對象返回公共方法)

是我的做法可以接受私人的功能呢?

+0

我不明白這種方法如何有用...有什麼優勢?這似乎令人困惑。爲什麼不只是'A.prototype = {...}' – elclanrs 2012-08-16 11:07:25

+0

將原型設置爲和對象相同的操作不能用於擴展它。 – lietus 2012-08-16 11:45:32

回答

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 (...) {...}; 
} 
+0

我想故意避免在構造函數中聲明方法,因爲它們是爲每個新實例創建的。 – lietus 2012-08-16 11:22:39

+0

更多信息:http://blog.thejit.org/2010/10/10/javascript-class-performance/ 構造函數性能的私有函數大約減少了98% – lietus 2012-08-17 12:59:53

0

自從你問了兩年多了,但在谷歌搜索一個類似的方法,我最終在這裏。除了(因爲你實質上是在徵求意見)之外,我沒有看到你的實現有什麼缺點,它似乎有點混淆你爲什麼將原型作爲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