mgusmano / sharpkit

Automatically exported from code.google.com/p/sharpkit
0 stars 0 forks source link

Support native javascript get/set properties #181

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
    Contact.prototype.__defineGetter__("Name", function()
    {
        alert("Get");
    });
    Contact.prototype.__defineSetter__("Name", function (ggg) {
        alert("Set" + ggg);
    });

and in JSON:
var lost = {
    loc : "Island",
    get location () {
        return this.loc;
    },
    set location(val) {
        this.loc = val;
    }
};
lost.location = "Another island";

Original issue reported on code.google.com by DanelK...@gmail.com on 4 Aug 2012 at 9:01

GoogleCodeExporter commented 8 years ago
Please don't use "__defineGetter__", its for FireFox and I think it already 
deprecated. does not support by IE9 too.
The following syntax is working in all modern browser and IE9 too.

        Object.defineProperty(object, "propertyName", {
            get:function () {
                return this._value;
            },
            set:function (value) {
                this._valye = value;
            }
        });

the JSON format work in all modern browser including IE9.

Original comment by Madnik7@gmail.com on 4 Aug 2012 at 10:11

GoogleCodeExporter commented 8 years ago

Original comment by DanelK...@gmail.com on 28 Aug 2012 at 6:54

GoogleCodeExporter commented 8 years ago
I just fix my sample

    var Foo = function()
    {
        this._test = 1;
    };

    Object.defineProperty(Foo.prototype, "test", {
            get:function () {
                return this._test;
            },
            set:function (value) {
                this._test = value;
            }
        });

Original comment by Madnik7@gmail.com on 15 Sep 2012 at 10:11

GoogleCodeExporter commented 8 years ago
Please don't forget to set "enumerable : true"

Object.defineProperty(Foo.prototype, "test", {
            get:function () {
                return this._test;
            },
            set:function (value) {
                this._test = value;
            },
            enumerable : true //*** HERE
        });

Original comment by Madnik7@gmail.com on 15 Sep 2012 at 6:48

GoogleCodeExporter commented 8 years ago
I find serious problem in SharpKit. the following approach generally not work 
for new get set properties. 
ce.prototype[p], ce2.prototype[p] will call property function and value not its 
and not copy its property handler, it cause serious issue. I wonder how you 
going to fix it! :(

if (typeof($Inherit)=='undefined') {
    var $Inherit = function(ce, ce2) {
        for (var p in ce2.prototype)
            if (typeof(ce.prototype[p]) == 'undefined' || ce.prototype[p]==Object.prototype[p])
                ce.prototype[p] = ce2.prototype[p]; /**
        for (var p in ce2)
            if (typeof(ce[p]) == 'undefined')
                ce[p] = ce2[p];
        ce.$baseCtor = ce2;
    }
}

Original comment by Madnik7@gmail.com on 16 Sep 2012 at 10:55

GoogleCodeExporter commented 8 years ago
I find Object.Create is naively support inheritance, but I don't have idea how 
to use it. mean while I found a solution same as your $inherit method. I didn't 
test it much.

$inherit(ce, ce2)
{
var props = Object.getOwnPropertyNames(ce2.prototype);
                for (var i = 0; i < props.length; i++)
                    if (typeof(Object.getOwnPropertyDescriptor(ce.prototype, props[i])) == 'undefined')
                        Object.defineProperty(ce.prototype, props[i], Object.getOwnPropertyDescriptor(ce2.prototype, props[i]));

                for (var p in ce2)
                    if (typeof(ce[p]) == 'undefined')
                        ce[p] = ce2[p];
                ce.$baseCtor = ce2;
}

regards

Original comment by Madnik7@gmail.com on 16 Sep 2012 at 12:22

GoogleCodeExporter commented 8 years ago
getOwnPropertyNames is very nice for new browsers, although it's not supported 
on older browsers... only IE9+

But I will investigate this issue further

Original comment by DanelK...@gmail.com on 17 Sep 2012 at 12:30

GoogleCodeExporter commented 8 years ago

Original comment by DanelK...@gmail.com on 23 Sep 2012 at 5:41

GoogleCodeExporter commented 8 years ago

Original comment by sebastia...@gmail.com on 26 Sep 2012 at 11:41

GoogleCodeExporter commented 8 years ago

Original comment by sebastia...@gmail.com on 27 Sep 2012 at 6:43

GoogleCodeExporter commented 8 years ago
Found two issue in v4:30

1)
Assembly NativeProperty does not affect!
[assembly: JsProperty(NativeProperty = true)]

2)
Your defined property has not set enumerable to true. enumerable: false"
I wrote in Sharpkit Group too:
https://groups.google.com/forum/#!topic/sharpkit/MTxkEaURj7s

Still I couldn't use it , currently I use my own implementation, but it is not 
welcome. My implementation is here:
http://code.google.com/p/shetabebookextras/source/browse/trunk/Agents/Html/Commo
n/SharpKitMiss.cs

Regards

Original comment by Madnik7@gmail.com on 4 Oct 2012 at 7:37

GoogleCodeExporter commented 8 years ago
fixed

Original comment by sebastia...@gmail.com on 4 Oct 2012 at 11:08

GoogleCodeExporter commented 8 years ago
Thanks for new update. I found you create native property for static 
propertiest too and I didn't know that is possible, that's great but I found 
the the issue in $inherit method while NativePropertiesEnumerable is true and 
base class has static property.

here is new code to fix it:
regards

if (typeof ($Inherit) == 'undefined') {
    var $Inherit = function (ce, ce2) {

        if (typeof (Object.getOwnPropertyNames) == 'undefined') {

            for (var p in ce2.prototype)
                if (typeof (ce.prototype[p]) == 'undefined' || ce.prototype[p] == Object.prototype[p])
                    ce.prototype[p] = ce2.prototype[p];
            for (var p in ce2)
                if (typeof (ce[p]) == 'undefined')
                    ce[p] = ce2[p];
            ce.$baseCtor = ce2;

        } else {

            var props = Object.getOwnPropertyNames(ce2.prototype);
            for (var i = 0; i < props.length; i++)
                if (typeof (Object.getOwnPropertyDescriptor(ce.prototype, props[i])) == 'undefined')
                    Object.defineProperty(ce.prototype, props[i], Object.getOwnPropertyDescriptor(ce2.prototype, props[i]));

            props = Object.getOwnPropertyNames(ce2);
            for (i = 0; i < props.length; i++)
                if (typeof (Object.getOwnPropertyDescriptor(ce, props[i])) == 'undefined')
                    Object.defineProperty(ce, props[i], Object.getOwnPropertyDescriptor(ce2, props[i]));

            ce.$baseCtor = ce2;

        }

    }
};

Original comment by Madnik7@gmail.com on 15 Oct 2012 at 2:43