あなたが尋ねてから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