override toString for Object

K

kirkox

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.
 
V

VK

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);
 
K

kirkox

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.
 
R

Richard Cornford

kirkox said:
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.
 
P

Peter Michaux

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top