473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

override toString for Object

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
8 8757
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
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
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
Ok now is ok.

bye

Feb 13 '07 #5
Thanks for your reply Richard.

Feb 13 '07 #6
thanks for your reply Richard.

bye

Feb 13 '07 #7
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Craig Schneider | last post by:
// Is there any way to override the XML Serialization of the following SimpleClass // to turn off the DefaultValue of a boolean? Sure, I can override the DefaultValue from // true to false, but...
7
by: AWHK | last post by:
How can I force anyone who subclasses my class to override the ToString() method? Andreas :-)
8
by: Sunny | last post by:
Hi all, The code as follow: using System; class A { public virtual void F() {
3
by: portroe | last post by:
How Do i override the method toString()? Now it should return a string representation of an object, with various variables such as Lastname, Age and Marital status thanks Portroe
3
by: Tony Johansson | last post by:
Hello!! You may correct me if I have made any wrong assumptions. Below I have some simple classes. When you have this t.ToString() below it's the ToString() method in Object class that is...
3
by: Publicjoe | last post by:
OK Folks, I am one confused puppy. Can someone please explain what the difference is and when to use each. I have a form with a button dropped onto it. Without actually doing anything to the...
2
by: Publicjoe | last post by:
OK Try again Can someone please explain what the difference is and when to use each. I have a form with a button dropped onto it. Without actually doing anything to the button, I next add the...
8
by: Phil Jollans | last post by:
Hi, I am having difficulty overriding the ToString() method of CultureInfo using Visual Studio 2005. Exactly the same code works fine with Visual Studio .NET 2003. What I am doing is adding...
5
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.