Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

override toString for Object

0 views
Skip to first unread message

kirkox

unread,
Feb 13, 2007, 3:35:33 PM2/13/07
to
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.

VK

unread,
Feb 13, 2007, 3:59:05 PM2/13/07
to
On Feb 13, 11:35 pm, "kirkox" <mircovel...@gmail.com> 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);


kirkox

unread,
Feb 13, 2007, 4:23:24 PM2/13/07
to
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.


Richard Cornford

unread,
Feb 13, 2007, 5:52:23 PM2/13/07
to
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.

kirkox

unread,
Feb 13, 2007, 6:27:52 PM2/13/07
to
Ok now is ok.

bye

kirkox

unread,
Feb 13, 2007, 6:32:49 PM2/13/07
to
Thanks for your reply Richard.

Message has been deleted

Douglas Crockford

unread,
Feb 13, 2007, 8:55:30 PM2/13/07
to
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/

Peter Michaux

unread,
Feb 14, 2007, 2:16:17 AM2/14/07
to

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

0 new messages