JavaScript class prototyping using module pattern
I was searching of way to define a class in JavaScript. I came up with hybrid Module and Prototype pattern, but not sure if I don't miss something. Basically I wanted to use 'this' keyword. Example:
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();
Return value is just for readability - A.prototype can be used in the beginning.
Patterns I also tried:
Is my approach acceptable?
**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 (...) {...};
}
It's over two years since you asked, but in Googling for a similar approach I ended up here. I don't see a drawback to your implementation other than (since you are essentially asking for an opinion) it seeming a tad confusing why you are passing the prototype in as an import on the IIFE.
Otherwise, what you've got looks very similar to other "standard" implementations of the "Revealing Prototype Pattern" which I've seen essentially as such:
(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();
There is also a similar module pattern, the structure of which might be more like the "Class" implementation you are after:
(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();
Gist: https://gist.github.com/dgowrie/24fb3483051579b89512
链接地址: http://www.djcxy.com/p/50132.html上一篇: JavaScript揭示模块模式的缺点
下一篇: 使用模块模式的JavaScript类原型