By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,826 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,826 IT Pros & Developers. It's quick & easy.

override toString for Object

P: n/a
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...bject:toString

or....?

Please, help me.

Thanks in advance for the help.

See you.

Feb 13 '07 #1
Share this Question
Share on Google+
8 Replies

P: n/a
VK
On Feb 13, 11:35 pm, "kirkox" <mircovel...@gmail.comwrote:
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);
Feb 13 '07 #2

P: n/a
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.
Feb 13 '07 #3

P: n/a
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.

Feb 13 '07 #4

P: n/a
Ok now is ok.

bye

Feb 13 '07 #5

P: n/a
Thanks for your reply Richard.

Feb 13 '07 #6

P: n/a
thanks for your reply Richard.

bye

Feb 13 '07 #7

P: n/a
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/
Feb 14 '07 #8

P: n/a
On Feb 13, 5:55 pm, Douglas Crockford <nos...@sbcglobal.netwrote:
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

Feb 14 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.