0

extending Object causes epic fail?

Can anyone think of a reason why this wouldn't work? This seems be causing my entire widget to fail in the loading process.

I've tested this in a browser and it works fine. I even run it in the terminal and it works. As soon as I include it in my widget, however, everything breaks.

CODE
(function() {
Object.prototype.serialize = function() {
var str = [];
for(var p in this) if (typeof this[p] != 'function') str.push(p + "=" + encodeURIComponent(this[p]));
return str.join("&");
};
})();


Ubuntu:9.10
KONtx: 1.0.0.0

by
10 Replies
  • sorry

    KONtx 1.2
    wdk 1.0.0.0
    0
  • I've seen issues myself modifying the prototype of the built in objects, so I tend to leave the prototype alone. I'm not sure how it may effect other widgets as well.

    If you comment the code of your method, does it still fail? In other words, does it appear to be the modifying of the prototype that causes the issue, or the code inside?
    0
  • Ah, thanks for the suggestion.

    It appears to be the line modifying the prototype, rather than the function itself. I'll try rewriting it as a more generic function and see if that works... it's a shame, though. I tend to find prototype extensions much more readable and easy to maintain in the long run.

    I guess I can map it to $serialize() or something like that.
    0
  • Any reason you are serializing using a custom method instead of using JSON? JSON.stringify is the preferred method for serializing objects.
    0
  • Unfortunately I don't have control over the API feed. I would love it if I could use JSON... the parsing would be easier as well.

    Thanks for the suggestion, though.
    0
  • Actually, I totally misunderstood your suggestion. I think I will use JSON.stringify. I just wish I could get JSON back...
    0
  • QUOTE (sax @ Feb 4 2010, 01:09 PM) <{POST_SNAPBACK}>
    Actually, I totally misunderstood your suggestion. I think I will use JSON.stringify. I just wish I could get JSON back...


    You might want to look into using Yahoo! Pipes to be able to transform the data from whatever format it's in into JSON.

    -Jeremy
    0
  • Hi sax,

    QUOTE (sax @ Feb 1 2010, 11:04 AM) <{POST_SNAPBACK}>
    Can anyone think of a reason why this wouldn't work? This seems be causing my entire widget to fail in the loading process.

    I've tested this in a browser and it works fine. I even run it in the terminal and it works. As soon as I include it in my widget, however, everything breaks.

    CODE
    (function() {
    Object.prototype.serialize = function() {
    var str = [];
    for(var p in this) if (typeof this[p] != 'function') str.push(p + "=" + encodeURIComponent(this[p]));
    return str.join("&");
    };
    })();


    I ran the following code and had no problems:

    CODE
    updateView: function () {

    (function() {
    Object.prototype.serialize = function () {
    var str = [];
    for (var p in this) if (typeof this[p] != 'function') str.push(p + "=" + encodeURIComponent(this[p]));
    return str.join("&");
    };
    })();

    var o = {
    firstName: "Joe",
    lastName: "Public",
    team: "Phillies"
    };
    log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
    log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
    log(o.serialize());
    log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
    log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

    }


    I'm not sure why you wrapped your code in a self-executing anonymous function, but I'm sure there was a reason. I just wanted to follow up and say that there's nothing preventing you from extending the native types.

    My $.02

    - Ben
    0
  • QUOTE (Benjamin Toll @ Mar 8 2010, 06:33 PM) <{POST_SNAPBACK}>
    Hi sax,



    I ran the following code and had no problems:

    CODE
    updateView: function () {

    (function() {
    Object.prototype.serialize = function () {
    var str = [];
    for (var p in this) if (typeof this[p] != 'function') str.push(p + "=" + encodeURIComponent(this[p]));
    return str.join("&");
    };
    })();

    var o = {
    firstName: "Joe",
    lastName: "Public",
    team: "Phillies"
    };
    log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
    log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
    log(o.serialize());
    log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
    log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

    }


    I'm not sure why you wrapped your code in a self-executing anonymous function, but I'm sure there was a reason. I just wanted to follow up and say that there's nothing preventing you from extending the native types.

    My $.02

    - Ben

    So does each widget get it's own Javascript execution/environment where any modification to built-in object would not affect other widgets?
    0
  • Hi Steve,

    QUOTE (Steve @ Mar 9 2010, 03:37 AM) <{POST_SNAPBACK}>
    So does each widget get it's own Javascript execution/environment where any modification to built-in object would not affect other widgets?


    Yes, that's correct.

    You know, I was messing around some more with augmenting the native types. I discovered that while my code above works, if I then go to another view my widget will throw an exception. This could have been what sax was referring to. This is because augmenting Object.prototype with a function will throw an exception when parsed by the JSON object. We're looking into it. Digging a little more, I also discovered that augmenting Object.prototype with a non-function data type throws an exception in the framework. This will be fixed in a future release.

    So, in light of everything, my suggestion is to not augment native types. As an alternative, bind the method to the object itself as a class method, i.e.:

    CODE
    Object.serialize = function (o) {
    var str = [];
    for (var p in o) if (typeof o[p] != 'function') str.push(p + "=" + encodeURIComponent(o[p]));
    return str.join("&");
    };

    var o = {
    firstName: "Joe",
    lastName: "Public",
    team: "Phillies"
    };

    Object.serialize(o);


    But as Jeremy said above, using JSON for serializing is really the best choice.

    - Ben
    0

Recent Posts

in General - Yahoo! TV Widgets