Results 1 to 10 of 10

Thread: Prototypes Methods on Native Objects

  1. #1
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Prototypes Methods on Native Objects

    Code:
    Number.prototype.valueOf = function() {return 1};
    Number.prototype.toString = Number.prototype.valueOf;
    var x = new Number(7);
    alert(x); //Alerts 1
    How would I instead be able to have those two methods return let's say 2 times the value of the number

    Code:
    Number.prototype.valueOf = function() {return /* Magic Code...something like (this*2) */};
    Number.prototype.toString = Number.prototype.valueOf;
    var x = new Number(7);
    alert(x); //Should alert 14
    Note: While I want to know how to do something like this in general for predefined objects, I know that the current example is not very practical.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Either:

    Code:
    Number.prototype.v = function() {return this*2};
    Number.prototype.toString = Number.prototype.v;
    var x = new Number(7);
    alert(x); //Alerts 14
    or:

    Code:
    Number.prototype.toString = function() {return this*2};
    var x = new Number(7);
    alert(x); //Alerts 14
    I think that co-opting the valueOf method as a prototype is problematical when performing an operation such as multiplication due to the native action of valueOf. This appears to send the browser into an endless loop.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    I didn't realize it was too much recursion. All I got was some baffling exception error. Thanks for pointing me in the right direction. I got it to work with valueOf().
    Code:
    Number.prototype.nativeValueOf = Number.prototype.valueOf;
    Number.prototype.valueOf = function() {
    	return this.nativeValueOf()*2;
    	}
    Number.prototype.toString = function() {return this};
    var x = new Number(7);
    alert(x); //Alerts 14
    Though it does not work if I define x like the following:
    Code:
    var x = Number(7);
    //or
    var x = 7;
    I guess that's a good thing because then I do return this.nativeValueOf()*2, then I would have issues with the scalar 2 and also the 7 within the new Number(7).
    Last edited by Trinithis; 05-26-2007 at 03:14 PM.

  4. #4
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Oh, and one more thing, how can I do something like this:
    Code:
    Number.prototype.value = /*Something that equals this... all I keep getting is [object Window]*/
    var x = new Number(7);
    alert(x.value); //Should alert 7
    This should be a property and not a method.
    Last edited by Trinithis; 05-26-2007 at 03:39 PM.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    As far as I can tell and have seen, an object's prototype properties are set globally and can then be set individually, only after the individual object is initialized (or during the initialization process if an initialization function - a custom object constructor is used):

    Code:
    Number.prototype.value = 0;
    var x = new Number(7);
    x.value+=x;
    alert(x.value); // alerts 7
    There may well be some way around this requirement though. If you know of an example that shows this to be otherwise with some other object, it may be able to be adapted. The this keyword, when used in the global scope, should return the window object, just as you found that it did.
    Last edited by jscheuer1; 05-26-2007 at 05:47 PM. Reason: precision
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    But the value and string value of all numbers should not be twice the value of the actual number. Also, toString() should return a string.
    Number.prototype.value = /*Something that equals this... all I keep getting is [object Window]*/
    var x = new Number(7);
    alert(x.value); //Should alert 7
    Yes, as John said you can't reference an instance before it's been created. You could use a factory method:
    Code:
    Number.withX = function() {
      var n = Number.apply(null, Array.prototype.slice.call(arguments));
      n.value = n;
      return n;
    };
    
    var myNewNum = Number.withX(7);
    myNewNum.value; // 7
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    alert(myNewNum.value);
    returns undefined.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ah, yes, passed by value. That's irritating. OK:
    Code:
    Number.withSquare = function() {
        var n = Number.apply(null, Array.prototype.slice.call(arguments));
        n.square = g * g;
        return {p:n};
    };
    
    var g = Number.withX(5);
    g.p; // 5
    g.p.square; // 25
    Not quite as convenient, alas.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That (as written, and alerting the results), doesn't do anything. If done like so:

    Code:
    Number.withSquare = function() {
        var n = Number.apply(null, Array.prototype.slice.call(arguments));
        n.square = g * g;
        return {p:n};
    };
    
    var g = Number.withSquare(5);
    alert(g.p); // 5
    alert(g.p.square); // 25
    I get 5 and undefined.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    How about:

    Code:
    Number.withSquare = function(n) {
        return {value:n, square:n*n};
    };
    
    var g = Number.withSquare(5);
    alert(g.value); // 5
    alert(g.square); // 25
    But, with any of these, if Number.withSquare (or whatever) is a function, g is an object and will return as such:

    Code:
    alert(g);
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •