0

I have one here:

http://jsfiddle.net/s9dLb/2/

What I noticed is that in this framework, all variables are static ( they are on the prototype chain ), and are only "instance" based if you set them in the constructor.

I'm using these terms from CS101 usually taught via Java.

Is there a better way to do this?

By default I set instance variables to a string 'instance' which will be replaced once the constructor is called.

var klass = function (obj, config_module) {
    if (config_module === 'constructor') {
        var object_public;
        if (obj.constructor) {
            object_public = obj.constructor;
            delete obj.constructor;
        }
        _.some(obj, function (val, key) {
            object_public.prototype[key] = val;
        });
        return object_public;
    }
};

var Test = klass({
    // instance, set in constructor
    A : 'instance',


    // static, not set in constructor
    B :  4,

    constructor: function (some_var) {
        this.A = some_var;
    },
    get: function () {
        return 2;
    },
    set: function () {
    }
}, 'constructor');


var instanceA = new Test('A');
var instanceB = new Test('B');

console.log('Start');
console.log(instanceA.A);
console.log(instanceB.A);
nativist.bill.cutting
  • 1,292
  • 3
  • 11
  • 19
  • Don't use `_.some` but `_.each` for iteration!!! – Bergi Dec 10 '13 at 22:12
  • 1
    No, there's no better way to do this. All instance properties need to be set in the constructor. Yet, "pre-initialising" them on the prototype is superfluous. – Bergi Dec 10 '13 at 22:14
  • It's a class system, so same way you should be able to see instance variables at the top of your class, you can see them. It's good for readability. I realize it does not need to be there, but I like the visibility. I could just use a comment instead of the string initialization, but I definitely want them there. – nativist.bill.cutting Dec 10 '13 at 22:16
  • Just start the "class" with the constructor (which [usual classes](http://stackoverflow.com/a/13418980/1048572) do anyway) and you can see the instance properties there. – Bergi Dec 10 '13 at 22:21
  • O.K. All my properties are public is there any way to make them private? – nativist.bill.cutting Dec 10 '13 at 22:24
  • Yes, there is, via creating scoped variables in the constructor. Read the post I linked above. – Bergi Dec 10 '13 at 22:28
  • public/private/static: http://jsfiddle.net/3ZDfQ/ – ebaranov Dec 10 '13 at 22:41
  • @Bergi - not sure this is a good hack. The private variable you create is not accessible from the class methods. Also, to access it, you create a method for each instance, not for each class - not very efficient to create the same method for each object. But I do see your point. Also, this can not be added to a framework as it is all contained with in the constructor function. i.e. I can't abstract it further. Good to know this is the alternate path though. – nativist.bill.cutting Dec 11 '13 at 00:55
  • ... the private variable is either not accessible at all or accessible from a getter function which is available to both the class methods and the outside world...this does not emulate "class" privacy very much or at all ... my initial scan of the post was correct. – nativist.bill.cutting Dec 11 '13 at 01:14

1 Answers1

1

Give MooTools a try. It is as close as it can get without the use of other languages which compile to JavaScript (like CoffeScript or TypeScript).

Here is an example MooTools class:

Foo = new Class({

    // instance variable:
    name: '',

    // construtor:
    initialize: function(name) {
        this._setName(name);
    },

    // instance method:
    getName: function(){
        return this.name;
    },

    // protected instance method:
    _setName: function(name) {
        this.name = name; 
    }.protect()

});

// static variable:
Foo.bar = 'bar';

// static method
Foo.bar = function(){}

// instance object:
var myFoo = new Foo('my new name');
var theName = myFoo.getName();

var checkInstance = instanceOf(myFoo, Foo);

Fiddle

Maciej Sz
  • 11,151
  • 7
  • 40
  • 56