LinkedInAttic / Fiber

Lightweight JavaScript prototypal inheritance model
Apache License 2.0
280 stars 32 forks source link

Properties support #15

Open QuentinRoy opened 10 years ago

QuentinRoy commented 10 years ago

I guess everything is in the title. I would love to have this.

krisk commented 10 years ago

@QuentinRoy, just to avoid confusion, can you be a little specific? Thanks!

QuentinRoy commented 10 years ago

I was actually talking about the built in getter and setter mechanism (http://ejohn.org/blog/javascript-getters-and-setters/).

For example I would enjoy being able to do stuff like this:

var PropertyTest = Fiber.extend(function () {

    return {

        init: function (stuff) {
            this.stuff = stuff;
        },

        get stuffAnd2() {
            return this.stuff + 2;
        },

        /*
        set stuffAnd2(val) {
            this.stuff = val - 2;
        }
        */

    };

});

var pTest = new PropertyTest(4);

console.log(pTest.stuffAnd2);
// Should returns 6 but returns 'NaN'.
// Instead, the getter is actually called only once, before the initialization,
// and the returned value is copied in the instance instead of the getter.

pTest.stuffAnd2 = 8;
// Should throw an error because the setter is commented (making stuffAnd2 read-only).
// Instead it overwrites stuffAnd2.

pTest.stuff = 0;
console.log(pTest.stuffAnd2);
// Still prints 8 while it should be 2 now.