- 139
- 10
[var | Namespace.]ClassName = (function(Class) {
/* Any static(class-wide) privates go here */
var privateMethod = function() {
return 'I\'m doing something privately';
};
/* The class prototype */
Class.prototype = {}; // or new BaseClass();
Class.prototype.propertyName = function(value) {
var returnValue;
// Unlock private Access
this.privateAccess = true;
// Forward the property access
returnValue = this.privateAccessFunction('propertyName', value);
// Lock it back up again!
delete this.privateAccess;
return returnValue;
};
// Lock the function, so it can't be modified.
Object.freeze(Class.prototype.propertyName);
// Optional: Lock the prototype
/* The class constructor */
Class.instance = function(config) {
// Fail if called without using new!
if (!this instanceof Class)
return undefined;
// So derivative classes don't share privates
if (config) {
var myPrivateMembers = {
propertyName: 'value'
};
Object.defineProperty(
this,
'privatePropertyAccess''privateAccessFunction',
{
//This is the actual Getter/Setter
// P.S. I like chainable properties
value: function(name, value) {
// Protects our function from call(obj)
if (!this.privateAccess || !this instanceof Class)
return undefined;
if (name !== undefined && typeof name === 'string') {
if (value !== undefined) {
myPrivateMembers[name] = value;
return this;
}
return myPrivateMembers[name];
}
return undefined;
},
writable: false,
configurable: false,
enumerable: false
}
);
}
};
// So that we get the Class back
return Class;
}([Namespace.]ClassName = [Namespace.]ClassName || function(config){
if (this instanceof [Namespace.]ClassName)
return [Namespace.]ClassName.instance.call(this, config);
}));
[var | Namespace.]ClassName = (function(Class) {
/* Any static(class-wide) privates go here */
var privateMethod = function() {
return 'I\'m doing something privately';
};
/* The class prototype */
Class.prototype = {}; // or new BaseClass();
Class.prototype.propertyName = function(value) {
var returnValue;
// Unlock private Access
this.privateAccess = true;
// Forward the property access
returnValue = this.privateAccessFunction('propertyName', value);
// Lock it back up again!
delete this.privateAccess;
return returnValue;
};
// Lock the function, so it can't be modified.
Object.freeze(Class.prototype.propertyName);
// Optional: Lock the prototype
/* The class constructor */
Class.instance = function(config) {
// Fail if called without using new!
if (!this instanceof Class)
return undefined;
// So derivative classes don't share privates
if (config) {
var myPrivateMembers = {
propertyName: 'value'
};
Object.defineProperty(
this,
'privatePropertyAccess',
{
//This is the actual Getter/Setter
// P.S. I like chainable properties
value: function(name, value) {
// Protects our function from call(obj)
if (!this.privateAccess || !this instanceof Class)
return undefined;
if (name !== undefined && typeof name === 'string') {
if (value !== undefined) {
myPrivateMembers[name] = value;
return this;
}
return myPrivateMembers[name];
}
return undefined;
},
writable: false,
configurable: false,
enumerable: false
}
);
}
};
// So that we get the Class back
return Class;
}([Namespace.]ClassName = [Namespace.]ClassName || function(config){
if (this instanceof [Namespace.]ClassName)
return [Namespace.]ClassName.instance.call(this, config);
}));
[var | Namespace.]ClassName = (function(Class) {
/* Any static(class-wide) privates go here */
var privateMethod = function() {
return 'I\'m doing something privately';
};
/* The class prototype */
Class.prototype = {}; // or new BaseClass();
Class.prototype.propertyName = function(value) {
var returnValue;
// Unlock private Access
this.privateAccess = true;
// Forward the property access
returnValue = this.privateAccessFunction('propertyName', value);
// Lock it back up again!
delete this.privateAccess;
return returnValue;
};
// Lock the function, so it can't be modified.
Object.freeze(Class.prototype.propertyName);
// Optional: Lock the prototype
/* The class constructor */
Class.instance = function(config) {
// Fail if called without using new!
if (!this instanceof Class)
return undefined;
// So derivative classes don't share privates
if (config) {
var myPrivateMembers = {
propertyName: 'value'
};
Object.defineProperty(
this,
'privateAccessFunction',
{
//This is the actual Getter/Setter
// P.S. I like chainable properties
value: function(name, value) {
// Protects our function from call(obj)
if (!this.privateAccess || !this instanceof Class)
return undefined;
if (name !== undefined && typeof name === 'string') {
if (value !== undefined) {
myPrivateMembers[name] = value;
return this;
}
return myPrivateMembers[name];
}
return undefined;
},
writable: false,
configurable: false,
enumerable: false
}
);
}
};
// So that we get the Class back
return Class;
}([Namespace.]ClassName = [Namespace.]ClassName || function(config){
if (this instanceof [Namespace.]ClassName)
return [Namespace.]ClassName.instance.call(this, config);
}));
Update regarding Edit and Meta: (Since I cannot comment on the meta attached to this becuase my bounty threw me below the 50 pt requirement). After the first answer indicating an extra brace, it was indeed removed and the code is working. Some notes on the meta attached: The code here is not the same as the orginal question and was edited, despite the comments in the Meta. This is not an issue of refusal to fix code. Transferring an in practice and currently researched code piece to an abstract pseudocode like pattern sometimes has issues. This one had a curly brace.
Update regarding Edit and Meta: (Since I cannot comment on the meta attached to this becuase my bounty threw me below the 50 pt requirement). After the first answer indicating an extra brace, it was indeed removed and the code is working. Some notes on the meta attached: The code here is not the same as the orginal question and was edited, despite the comments in the Meta. This is not an issue of refusal to fix code. Transferring an in practice and currently researched code piece to an abstract pseudocode like pattern sometimes has issues. This one had a curly brace.
[var | Namespace.]ClassName = (function(Class) {
/* Any static(class-wide) privates go here */
var privateMethod = function() {
return 'I\'m doing something privately';
};
/* The class prototype */
Class.prototype = {}; // or new BaseClass();
Class.prototype.propertyName = function(value) {
var returnValue;
// Unlock private Access
this.privateAccess = true;
// Forward the property access
returnValue = this.privateAccessFunction('propertyName', value);
// Lock it back up again!
delete this.privateAccess;
return returnValue;
};
// Lock the function, so it can't be modified.
Object.freeze(Class.prototype.propertyName);
// Optional: Lock the prototype
/* The class constructor */
Class.instance = function(config) {
// Fail if called without using new!
if (!this instanceof Class)
return undefined;
// So derivative classes don't share privates
if (config) {
var myPrivateMembers = {
propertyName: 'value'
};
Object.defineProperty(
this,
'privatePropertyAccess',
{
//This is the actual Getter/Setter
// P.S. I like chainable properties
value: function(name, value) {
// Protects our function from call(obj)
if (!this.privateAccess || !this instanceof Class)
return undefined;
if (name !== undefined && typeof name === 'string') {
if (value !== undefined) {
privateMembers[name]myPrivateMembers[name] = value;
return this;
}
return privateMembers[name];myPrivateMembers[name];
}
return undefined;
},
writable: false,
configurable: false,
enumerable: false
}
);
}
};
// So that we get the Class back
return Class;
}([Namespace.]ClassName = [Namespace.]ClassName || function(config){
if (this instanceof [Namespace.]ClassName)
return [Namespace.]ClassName.instance.call(this, config);
}));
[var | Namespace.]ClassName = (function(Class) {
/* Any static(class-wide) privates go here */
var privateMethod = function() {
return 'I\'m doing something privately';
};
/* The class prototype */
Class.prototype = {}; // or new BaseClass();
Class.prototype.propertyName = function(value) {
var returnValue;
// Unlock private Access
this.privateAccess = true;
// Forward the property access
returnValue = this.privateAccessFunction('propertyName', value);
// Lock it back up again!
delete this.privateAccess;
return returnValue;
};
// Lock the function, so it can't be modified.
Object.freeze(Class.prototype.propertyName);
// Optional: Lock the prototype
/* The class constructor */
Class.instance = function(config) {
// Fail if called without using new!
if (!this instanceof Class)
return undefined;
// So derivative classes don't share privates
if (config) {
var myPrivateMembers = {
propertyName: 'value'
};
Object.defineProperty(
this,
'privatePropertyAccess',
{
//This is the actual Getter/Setter
// P.S. I like chainable properties
value: function(name, value) {
// Protects our function from call(obj)
if (!this.privateAccess || !this instanceof Class)
return undefined;
if (name !== undefined && typeof name === 'string') {
if (value !== undefined) {
privateMembers[name] = value;
return this;
}
return privateMembers[name];
}
return undefined;
},
writable: false,
configurable: false,
enumerable: false
}
);
}
};
// So that we get the Class back
return Class;
}([Namespace.]ClassName = [Namespace.]ClassName || function(config){
if (this instanceof [Namespace.]ClassName)
return [Namespace.]ClassName.instance.call(this, config);
}));
[var | Namespace.]ClassName = (function(Class) {
/* Any static(class-wide) privates go here */
var privateMethod = function() {
return 'I\'m doing something privately';
};
/* The class prototype */
Class.prototype = {}; // or new BaseClass();
Class.prototype.propertyName = function(value) {
var returnValue;
// Unlock private Access
this.privateAccess = true;
// Forward the property access
returnValue = this.privateAccessFunction('propertyName', value);
// Lock it back up again!
delete this.privateAccess;
return returnValue;
};
// Lock the function, so it can't be modified.
Object.freeze(Class.prototype.propertyName);
// Optional: Lock the prototype
/* The class constructor */
Class.instance = function(config) {
// Fail if called without using new!
if (!this instanceof Class)
return undefined;
// So derivative classes don't share privates
if (config) {
var myPrivateMembers = {
propertyName: 'value'
};
Object.defineProperty(
this,
'privatePropertyAccess',
{
//This is the actual Getter/Setter
// P.S. I like chainable properties
value: function(name, value) {
// Protects our function from call(obj)
if (!this.privateAccess || !this instanceof Class)
return undefined;
if (name !== undefined && typeof name === 'string') {
if (value !== undefined) {
myPrivateMembers[name] = value;
return this;
}
return myPrivateMembers[name];
}
return undefined;
},
writable: false,
configurable: false,
enumerable: false
}
);
}
};
// So that we get the Class back
return Class;
}([Namespace.]ClassName = [Namespace.]ClassName || function(config){
if (this instanceof [Namespace.]ClassName)
return [Namespace.]ClassName.instance.call(this, config);
}));
- 29k
- 11
- 86
- 188