Management of this in javascript

0 views
Skip to first unread message

Thomas

unread,
May 10, 2008, 1:52:23 PM5/10/08
to Ruby on Rails: Spinoffs
hello

I have a problem with the following code :

var Test = Class.create({
data:"Test",
initialize: function() {
this.t2 = new Test2(this.callt1);
},
callt1: function(){
alert(this.data);
}

});

var Test2 = Class.create({
data: "Test2",
initialize: function(callback) {
this.callback=callback;
},
callt2: function(){
this.callback();
}

});

var t = new Test();

t.t2.callt2();

I get "Test2" but I'm wait for "Test". I think it is not a bug but how
can I do to obtain "Test"

Thomas

kangax

unread,
May 10, 2008, 2:15:07 PM5/10/08
to Ruby on Rails: Spinoffs
That's quite confusing : )

You obtain "Test" like so:
t.t2.callback.call(t)

- kangax

darrin

unread,
May 10, 2008, 3:06:44 PM5/10/08
to Ruby on Rails: Spinoffs
Wow, is this a test?

You can also get "Test" by binding your callback to Test

var Test = Class.create({
data:"Test",
initialize: function() {
this.t2 = new Test2(this.callt1.bind(this));
},
callt1: function(){
alert(this.data);
}

});

Reply all
Reply to author
Forward
0 new messages