calling methods within a prototype (oo) way

T

tuka

Hi,

I have 2 methods defined within a javascript objects
that I would like to call in a third function within the same object.
In the example below, func3 works well in FF but in IE7 it breaks.

Any suggestions about how this may work ? I am sure this has to do
with notation.
I am calling the methods statically - so not instantiating a objects
first.

TIA,
Tuka

i.e.

in a prototype object

this.func1 = function(args) {
//do stuff
}

this.func2 = function(args) {
//do stuff
}

this.func3 = function(args) {
this.func1();
this.func2();
}
 
R

RobG

Hi,

I have 2 methods defined within a javascript objects
that I would like to call in a third function within the same object.
In the example below, func3 works well in FF but in IE7 it breaks.

Any suggestions about how this may work ? I am sure this has to do
with notation.
I am calling the methods statically - so not instantiating a objects
first.

TIA,
Tuka

i.e.

in a prototype object

this.func1 = function(args) {
//do stuff

}

this.func2 = function(args) {
//do stuff

}

this.func3 = function(args) {
this.func1();
this.func2();

}

It's always good to post working code (or at least code that, when
executed, demonstrates the problem or issue) that can be easily copied
and pasted.

The following works fine in Firefox and IE at least:

function Foo(){
}

Foo.prototype = {
func1: function(){alert('func1');},
func2: function(){alert('func2');},
func3: function(){this.func1();}
};


var foo = new Foo();
foo.func3(); // --> func1
 
T

Tom de Neef

RobG said:
It's always good to post working code (or at least code that, when
executed, demonstrates the problem or issue) that can be easily copied
and pasted.

The following works fine in Firefox and IE at least:

function Foo(){
}

Foo.prototype = {
func1: function(){alert('func1');},
func2: function(){alert('func2');},
func3: function(){this.func1();}
};

In above case "this" in "func3: function(){this.func1();}" refers to Foo.
But in the OP code:
this.func3 = function(args) {
this.func1();
this.func2();
}
what is the association of "this" in "this.func1();"? Will it always be to
the prototype object ?
Tom
 
R

RobG

In above case "this" in "func3: function(){this.func1();}" refers to Foo.

So certain are you. :)

You can't tell what a function's this keyword refers to by how it is
declared or initialised, only by how it's called - it doesn't have a
value until the function is called. When called as a method of foo,
func3's[1] this keyword refers to foo. Since foo doesn't have a func1
property, its prototype chain is searched and the property is found on
F's prototype (since foo was constructed by Foo).
But in the OP code:
this.func3 = function(args) {
    this.func1();
    this.func2();}

what is the association of "this" in "this.func1();"?

It is set according to the rules about how a function's this keyword
resolved and is entirely dependent on how the function is called.

Will it always be to the prototype object ?

No. The value of a function's this keyword is always determined by
the caller.


1. More correctly, the anonymous function referenced by func3.
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top