override toString for Object

Discussion in 'Javascript' started by kirkox, Feb 13, 2007.

  1. kirkox

    kirkox Guest

    Hi guys, this is my first post here and I am a newbie, of course.

    My question is simple...I'd like to override toString() function such
    that it can works on Object.

    I have something like that

    var myObj = new Object();
    myObj.name = 'xxxx';
    myObj.surname = 'yyyy';
    .....
    ....

    How can I override toString function?

    There are some references on the net, like
    http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Object:toString

    or....?

    Please, help me.

    Thanks in advance for the help.

    See you.
    kirkox, Feb 13, 2007
    #1
    1. Advertising

  2. kirkox

    VK Guest

    On Feb 13, 11:35 pm, "kirkox" <> wrote:
    > Hi guys, this is my first post here and I am a newbie, of course.
    >
    > My question is simple...I'd like to override toString() function such
    > that it can works on Object.
    >
    > I have something like that
    >
    > var myObj = new Object();
    > myObj.name = 'xxxx';
    > myObj.surname = 'yyyy';
    > ....
    > ...
    >
    > How can I override toString function?


    Object object is the base for all other objects so by overriding
    Object methods you are automatically influencing all other current and
    future objects. So normally you do not touch Object itself, instead
    create your own class of objects with desired properties.

    function Person(name, surname) {
    this.name = name || "John";
    this.surname = surname || "Doe";
    }

    Person.prototype.toString = function() {
    return this.name + " " + this.surname;
    }

    var p1 = new Person;

    var p2 = new Person("Mary", "Brown");

    alert(p1);

    alert(p2);
    VK, Feb 13, 2007
    #2
    1. Advertising

  3. kirkox

    kirkox Guest

    Thanks for your reply.

    I have something like that:

    ///////////////////
    var copyoRow = new Array();

    function getValues(){
    ....
    for(var i = 0; i < numRows; i++) {
    oRow = new Object();
    oRow.name = ..getDocumentById('..');
    oRow.surname = getDocumentById('...'); // only for thie example

    copyRow.push(oRow);
    }
    ....
    return copyRow;
    }

    ....
    ////////////

    Now, I hve to to create a string to append to server request, so I
    need to using the properties of my Object.

    similar to... oRow.toString() and then copyRow.toString();

    Thanks a lot.
    kirkox, Feb 13, 2007
    #3
  4. kirkox wrote:
    > My question is simple...I'd like to override toString() function
    > such that it can works on Object.
    >
    > I have something like that
    >
    > var myObj = new Object();
    > myObj.name = 'xxxx';
    > myObj.surname = 'yyyy';
    > ....
    > ...
    >
    > How can I override toString function?

    <snip>

    By assigning a reference to a function object to the - toString -
    property of the object. That fucntion could, for example, be a named
    global function. E.G.:-

    function forObjectToString(){
    return (this.name + ' ' +t his.surname);
    }

    - and then:-

    myObj.toString = forObjectToString;

    - or evaluating a function expressions and assigning its result to the -
    toString - property of the object. E.G.:-

    myObj.toString = function(){
    return (this.name + ' ' +t his.surname);
    };

    Though in that case the function expression is possibly going to be an
    inner function and its assignment may form a closure. See:-

    <URL: http://jibbering.com/faq/faq_notes/closures.html >

    Richard.
    Richard Cornford, Feb 13, 2007
    #4
  5. kirkox

    kirkox Guest

    Ok now is ok.

    bye
    kirkox, Feb 13, 2007
    #5
  6. kirkox

    kirkox Guest

    Thanks for your reply Richard.
    kirkox, Feb 13, 2007
    #6
  7. kirkox

    kirkox Guest

    thanks for your reply Richard.

    bye
    kirkox, Feb 13, 2007
    #7
  8. kirkox wrote:
    > Hi guys, this is my first post here and I am a newbie, of course.
    >
    > My question is simple...I'd like to override toString() function such
    > that it can works on Object.


    > How can I override toString function?


    Object.prototype.toString = function () {
    return stringify(this);
    };

    See http://yuiblog.com/blog/2006/09/26/for-in-intrigue/
    Douglas Crockford, Feb 14, 2007
    #8
  9. On Feb 13, 5:55 pm, Douglas Crockford <> wrote:
    > kirkox wrote:
    > > Hi guys, this is my first post here and I am a newbie, of course.

    >
    > > My question is simple...I'd like to override toString() function such
    > > that it can works on Object.
    > > How can I override toString function?

    >
    > Object.prototype.toString = function () {
    > return stringify(this);
    > };
    >
    > Seehttp://yuiblog.com/blog/2006/09/26/for-in-intrigue/


    I don't think it is quite as straight forward in all cases. The
    specification of the language do allow for augmenting the Object
    prototype; however, doing so changes the way for-in loops can be used
    when function constructors are chained which is also allowed by the
    specifications.

    Object.prototype.foo = 123;

    function Person(name) {
    this.name = name;
    }
    Person.prototype.getName = function() {
    return this.name;
    };

    function Employee(name, department) {
    this.name = name;
    this.department = department;
    }
    Employee.prototype = new Person();
    Employee.prototype.getDepartment = function (id) {
    return this.department;
    };

    window.onload = function() {

    var giselle = new Employee('Giselle', 'lingerie');

    var props = [];
    for (var p in giselle) {
    props.push(p); // Oops foo gets pushed into props
    }
    console.log(props.join(', ')); // Firefox firebug

    props = [];
    for (var p in giselle) {
    // Yes filters out foo but also the name
    // and getName properties
    if (giselle.hasOwnProperty(p)) {
    props.push(p);
    }
    }
    console.log(props.join(', ')); // Firefox firebug
    }

    I think that you would suggest using parasitic inheritance for this
    but the argument here is what the specifications allow. Yes the above
    implemention is ugly but if random mashups are the concern then the
    above situation is in the mix to consider as well. In this case the
    use of hasOwnProperty filters out more than desired.

    Peter
    Peter Michaux, Feb 14, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ºa¤Ö
    Replies:
    0
    Views:
    448
    ºa¤Ö
    Jan 23, 2006
  2. Ryan Stewart
    Replies:
    3
    Views:
    7,612
    Roedy Green
    Sep 19, 2003
  3. Replies:
    3
    Views:
    795
    Tony Morris
    Jan 25, 2005
  4. narsing
    Replies:
    2
    Views:
    869
    Thomas Weidenfeller
    Oct 23, 2006
  5. Fergus McMenemie

    LibXML element->toString vs document->toString

    Fergus McMenemie, Jul 12, 2012, in forum: Perl Misc
    Replies:
    5
    Views:
    603
    Fergus McMenemie
    Jul 17, 2012
Loading...

Share This Page