Extending Built-in Javascript Objects - Mozilla

This is a discussion on Extending Built-in Javascript Objects - Mozilla ; Hi Guys, Inspired by prototype, where it Extended some of the native js objects, such as: Function.prototype.bind = function() { var __method = this, args = $A(arguments), object = args.shift(); return function() { return __method.apply(object, args.concat($A(arguments))); } } I tried ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Extending Built-in Javascript Objects

  1. Extending Built-in Javascript Objects

    Hi Guys,

    Inspired by prototype, where it Extended some of the native js objects, such
    as:

    Function.prototype.bind = function() {
    var __method = this, args = $A(arguments), object = args.shift();
    return function() {
    return __method.apply(object, args.concat($A(arguments)));
    }
    }

    I tried to do something like this:

    Node.prototype.isTextbox = function() {
    if (this.tagName == 'TEXTAREA') return true;
    if ('type' in this && this.tagName == 'INPUT') {
    var type = this.type;
    if (type == 'text'
    || type == 'password'
    || type == 'file') {
    return true;
    }
    }
    return false;
    };

    It is working fine if it is loaded from a web page, but when I load it into
    my extension, and do:
    var tbox = document.getElementById('a_text_box');
    alert(tbox.isTextBox());

    Then it error console get an error message saying that
    tbox.isTextBox is not a function

    but when I try to:
    for(i in tbox) log(i);
    I do see isTextBox is a method of tbox.

    Is there some kind of hidden restrictions that only applies to local script,
    and doesn't apply to remote script?

    If so, how do I get around it?

    Thanks a lot!

    Lei

  2. Re: Extending Built-in Javascript Objects

    Lei Sun wrote:
    > Hi Guys,
    >
    > Inspired by prototype, where it Extended some of the native js objects, such
    > as:
    >
    > Function.prototype.bind = function() {
    > var __method = this, args = $A(arguments), object = args.shift();
    > return function() {
    > return __method.apply(object, args.concat($A(arguments)));
    > }
    > }
    >
    > I tried to do something like this:
    >
    > Node.prototype.isTextbox = function() {
    > if (this.tagName == 'TEXTAREA') return true;
    > if ('type' in this && this.tagName == 'INPUT') {
    > var type = this.type;
    > if (type == 'text'
    > || type == 'password'
    > || type == 'file') {
    > return true;
    > }
    > }
    > return false;
    > };
    >
    > It is working fine if it is loaded from a web page, but when I load it into
    > my extension, and do:
    > var tbox = document.getElementById('a_text_box');
    > alert(tbox.isTextBox());
    >
    > Then it error console get an error message saying that
    > tbox.isTextBox is not a function
    >
    > but when I try to:
    > for(i in tbox) log(i);
    > I do see isTextBox is a method of tbox.
    >
    > Is there some kind of hidden restrictions that only applies to local script,
    > and doesn't apply to remote script?
    >
    > If so, how do I get around it?
    >
    > Thanks a lot!
    >
    > Lei


    It's likely because you aren't actually accessing a Node but a
    XPCNativeWrapper.
    See: http://developer.mozilla.org/en/docs/XPCNativeWrapper

    BUT, do not touch the prototypes of the built in Objects.
    This is a major cause of extension incompatibilities and might even
    break the application you're extending.

    Consider this example:
    Object.prototypes.somefunc = function() { ... };
    Now if another extension uses Object as a dict like so:
    var obj = {x: 1, y: 5};
    and the iterates over those items:
    for (i in obj) { alert(i); }
    it will yield something like:
    x, y, somefunc
    instead of the expected
    x, y

    Only do such things in you own (private) components or XUL windows.
    And in the latter case, make sure you don't break XUL itself, i.e. XBL
    widgets that rely on unmodified behavior.


    Nils


  3. Re: Extending Built-in Javascript Objects

    On 8/3/07, Nils Maier wrote:
    >
    > It's likely because you aren't actually accessing a Node but a
    > XPCNativeWrapper.
    > See: http://developer.mozilla.org/en/docs/XPCNativeWrapper
    >
    > BUT, do not touch the prototypes of the built in Objects.
    > This is a major cause of extension incompatibilities and might even
    > break the application you're extending.
    >
    > Consider this example:
    > Object.prototypes.somefunc = function() { ... };
    > Now if another extension uses Object as a dict like so:
    > var obj = {x: 1, y: 5};
    > and the iterates over those items:
    > for (i in obj) { alert(i); }
    > it will yield something like:
    > x, y, somefunc
    > instead of the expected
    > x, y
    >
    > Only do such things in you own (private) components or XUL windows.
    > And in the latter case, make sure you don't break XUL itself, i.e. XBL
    > widgets that rely on unmodified behavior.
    >
    >
    > Nils
    >
    > _______________________________________________
    > dev-extensions mailing list
    > dev-extensions@lists.mozilla.org
    > https://lists.mozilla.org/listinfo/dev-extensions
    >


    Yes. I am doing this in the private sidebar.xul of the extension, so
    it shouldn't go beyond to the upper windows.

    Yes, I had the xpcnativewrappers=no set in the chrome.manifest
    already, does it still mean that the object that I got back is a
    xpcnativewrapper?

    And note, that I am unable to do something with the local script,
    where I could do the same thing with the normal remote script. And the
    funny thing is I can see it if I enumerate it, but I get error when I
    run it.

    That's what didn't make any sense to me.

    Lei

+ Reply to Thread