0

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?

1 Answers1

2
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.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • I tried this is referring to DOMWindow when i check in firebug and in Google Chrome and even if i assign to self and pass self it will still be referring to DOMWindow any other suggestions Tomalak? –  Mar 15 '12 at 08:28
  • @Kathy See modified answer. I'm not sure which object you would like to pass to `new Grass()`. The position of `var self = this;` in the code was largely a guess. What *does* `Grass()` expect as the first parameter? – Tomalak Mar 15 '12 at 08:30
  • Grass is Expecting the World Class that's initialized i did pass World, and this no luck and i tried your suggestion assigning it to `self` and passing `self` too but no luck :( –  Mar 15 '12 at 08:35
  • [See this jsFiddle](http://jsfiddle.net/Tomalak/K56rB/1/). The above works for me, your error mut be somewhere else. – Tomalak Mar 15 '12 at 09:04
  • 1
    +1 - this is the correct pattern to use. you can also decorate the functions via `.bind(this)` but saving a reference is faster and has a smaller footprint. – Dimitar Christoff Mar 15 '12 at 09:38
  • @Dimitar How would that look like? (I know nothing about MooTools) – Tomalak Mar 15 '12 at 09:44
  • 2
    `...SelectMany(function(row) { ... return blah.Select(function(){ }.bind(this))}.bind(this)).toArray();` - function.bind is not mootools specific, it's actually now standard: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind - this is not advisable here as readability worsens a lot also due to the need to bind 2 functions just so you can religiously keep `this` bound to instance... – Dimitar Christoff Mar 15 '12 at 10:15
  • @Dimitar: Ah, I see. So `bind()` is a convenience wrapper around `call()`. Nice to know. – Tomalak Mar 15 '12 at 10:25
  • yep, binds a function that it can call/apply later as opposed to str away. – Dimitar Christoff Mar 15 '12 at 10:52