[Javascript] function object/custom object prototype confusion

Amol Katdare amol.forums at gmail.com
Wed Apr 13 15:41:36 CDT 2005


instead of
Person.prototype.toString() {
    return this.first + " " + this.last;
}

try the following:

function  toString()
{
    return this.first + " " + this.last;
}
Person.prototype.toString=toString;

The last statement tells the interpreter which function to really invoke 
when person's toString is called.
Or something like that.

Amol


james wrote:

> Hi
>
> I hope this is a simple question!!
>
> I've been following a javascript tutorial 
> (http://www.kevlindev.com/tutorials/javascript/inheritance/)
>
> right at the start, in the first few lines of the tutorial are some 
> lines of example code:
>
> // constructor function
> function Person(first, last) {
>     this.first = first;
>     this.last  = last;
> }
>
> // instance person of class Person
> var person = new Person("John", "Dough");
>
> // ???
> Person.prototype.toString() {
>     return this.first + " " + this.last;
> }
>
> // example
> alert( person.toString() ); // displays "John Dough"
> alert( person ); // same result since alert calls toString()
> --------------------
>
> but, I get an error for the line beginning Person.prototype.toString() 
> which looks wrong to me anyway (is a function or object literal?)
>
> can anyone with a bit more experience just take a quick peak and put 
> my out of my doubtful misery?
>
> James
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>




More information about the Javascript mailing list