Skip to main content
Notice removed Canonical answer required by CommunityBot
Bounty Ended with konijn's answer chosen by CommunityBot
deleted 2 characters in body; Post Made Community Wiki
Source Link
[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);
}));
deleted 581 characters in body
Source Link

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.

added 4 characters in body
Source Link
[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);
}));
edited body
Source Link
Loading
added 583 characters in body
Source Link
Loading
Code Edit - Removed extra brace
Source Link
Loading
edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Loading
Code Edit
Source Link
Loading
added 32 characters in body
Source Link
Loading
Notice added Canonical answer required by Fuzzical Logic
Bounty Started worth 100 reputation by Fuzzical Logic
Offering Bounty. Altered the code and question according to newer research.
Source Link
Loading
Tweeted twitter.com/#!/StackCodeReview/status/397527075105828864
Edited for clarity
Source Link
Loading
added 92 characters in body
Source Link
Loading
Source Link
Loading