Reputation:
I would like to know how to pass self to an other object in mootools, I am trying to build classes based on mootools class declaration, but i am noticing i cannot send the object itself using this when i use this it sends DOMWindow instead of World or the object itself, the following is my code,
var World = new Class({
....
initialize: function(rows, ...) {
// build grass // can only have one grass per location
this.grassList = Enumerable.Range(0, rows).SelectMany(
function(row) {
return Enumerable.Range(0, columns).Select(
function(column) {
return new Grass(this, new Location(row, column), quantityOfGrass, maxQuantityOfGrass, growRateOfGrass)
})
}).ToArray();
}
....
}
I'm facing problem at this location,
return new Grass(this, new Location(row, column), quantityOfGrass, maxQuantityOfGrass, growRateOfGrass)
since it didn't work i checked for,
return new Grass(World, new Location(row, column), quantityOfGrass, maxQuantityOfGrass, growRateOfGrass)
it didn't work either i am using linq.js and mootools could someone guide me?
Upvotes: 0
Views: 645
Reputation: 338228
var World = new Class({
....
initialize: function(rows, ...) {
// save a reference to the correct "this"
var self = this;
// build grass // can only have one grass per location
self.grassList = Enumerable.Range(0, rows).SelectMany(
function(row) {
return Enumerable.Range(0, columns).Select(
function(column) {
return new Grass(self, new Location(row, column), quantityOfGrass, maxQuantityOfGrass, growRateOfGrass)
})
}).ToArray();
}
....
}
The object referenced by this
changes dynamically.
A callback function like your function(column)
will know nothing about what this
referred to in functions called earlier. If you want to re-use the reference to a specific this
, you must save that reference in a variable.
Upvotes: 2