0

I use a custom framework as such:

var SForm = $A.support({

    Name: 'SForm',

    // instance based

    // hidden, created by using this

    // static based

    S: {
        google:    'https://plus.google.com/_/favicon?domain=',
        pre_url:   /(http:)|(https:)\/\//,
        domain:    /:\/\/(www\.)?([\.a-zA-Z0-9\-]+)/,

        // the good parts has good url regex - implement it some time

        url:       /:\/\/(www\.)?[\x00-\x7F]{1,1800}\.[\x00-\x7F]{1,200}/,
        email:     /\S{1,64}@[\x00-\x7F]{1,255}\.[\x00-\x7F]{1,255}/,
        tweet:     /\S{1,40}/,
        title:     /\S{1,32}/,
        name:      /\S{1,64}/,
        pass:      /\S{6,20}/,
        empty:     /\S+/
    },
    constructor : function (form_elements) {
        $A.someKey(form_elements, function (val) {
            if (val.type !== 'checkbox') {
                this.form_obj[val.name] = val.value;
            } else if (val.type === 'checkbox') {
                this.form_obj[val.name] = val.checked;
            }
        }, this);
    },

    // ... more 

This works for me because I can ape the Java class system a bit.

However I don't know how to make my instance variables explicit, like my static variables.

Here is the framework:

$P.block = $P.support = $P.parsel = function (obj, config_module) {
    $R.Parsel[obj.Name] = obj;

    // all properties are private

    if (!config_module) {
        return undefined;
    }

    // all properties are public

    if (config_module === true) {
        return obj;
    }

    // constructor based, all properties are public

    if (config_module === 'constructor') {
        var object_public;
        if (obj.constructor) {
            object_public = obj.constructor;
            delete obj.constructor;
        }
        $A.someKey(obj, function (val, key) {
            if (/^s_/.test(key)) {
                object_public[key] = val;
            } else if (/^p_/.test(key)) {
                object_public.prototype[key] = val;
            } else {
                object_public.prototype[key] = val;
            }

        });
        return object_public;
    }
};
employee-0
  • 1,023
  • 1
  • 9
  • 19
  • that's why these questions get closed b.c. they cause debate, some people like to ape the Java class system, some don't. Neither is right or wrong. What is wrong, is debating it, instead of just helping. – employee-0 Nov 03 '13 at 01:48
  • Not sure what it is you're trying to do. Are you trying to create private instance specific variables that you can access from prototype? If so then you can't. If you want to use instance specific private variables then forget about prototype (bad idea) Or use the `this._myprivate` syntax like many others do. http://stackoverflow.com/a/16063711/1641941 – HMR Nov 03 '13 at 01:54
  • Not sure if you replied to me but those are the options you have. If you need private instance specific and you have functions that need to access them then you can't put those functions in the prototype. Non instance specific privates you can have them in the prototype (link posted under "For privates that don't need to be instance specific"). If you want to increase memory usage and cpu cost to simulate privates in JS than that's entirely up to you. I don't know the design of your project. Personally I've never had a situation where it was needed though. – HMR Nov 03 '13 at 03:15

1 Answers1

1

I'm not quite sure what your question is asking, but looking at your previous questions it looks like you're either avoiding, or are not aware of, the object oriented nature of JavaScript?

Here's an example, I hope it helps you.

// Define constructor function
function Child(name) {
    // Check if "static" count exists.
    if (typeof Child.count === "undefined") Child.count = 0;

    // Assign instance variable
    this.name = name;
    Child.count++;
}

var alice, bob;

alice = new Child("Alice");
console.log(alice.name) // ==> "Alice"
console.log(Child.count) // ==> 1

bob = new Child("Bob");
console.log(alice.name) // ==> "Bob"
console.log(Child.count) // ==> 2

Update: Using closures

If you really want have private variables that inaccessible outside of the function, you can use a closure to limit the scope of the variable to that scope.

var Child = (function() {
    // Private static variable
    var count = 0;

    function constructor(name) {
        // Public static variable
        constructor.likes = ['toys', 'candy', 'pets', 'coloring'];

        // Instance variable
        this.name = name;

        // Accessor methods
        this.getCount = function() {
            return count;
        }

        this.getLikes = function() {
            return constructor.likes;
        }

        count++;
    }

    // Return the constructor function
    return constructor;
})()
Community
  • 1
  • 1
thgaskell
  • 12,772
  • 5
  • 32
  • 38
  • I'll go ahead and mark this. But I don't have any indication that you read my code above. Do you see how I've implemented purely private and purely public classes? I should have called $A.support, $A.klass. – employee-0 Nov 03 '13 at 21:59
  • But to respond, I am aware of the OO nature of JavaScript, by itself I find it not useful, and there are many ways to build upon it. Obviously, I like what I built as I can build completely private modules, and completely public modules. – employee-0 Nov 03 '13 at 22:00
  • I updated the answer to show how you can achieve private and public static variables using closures which is on the functional side of javascript, but still leveraging the OO nature to provide instance variables. – thgaskell Nov 06 '13 at 03:41