开发者

Calling a function of a 'super' javascript object

I have an ob开发者_开发技巧ject structure/hierarchy like the one below:


var obj = {

    dimensions : {

        x : 100,
        y : 100,
        w : 300,
        h : 400,
        cmp : function () {

            return this.x + this.y;

        }

    },

    definition : {

        base : {

            rect1 : {

              // how do I get the value of dimensions.x ??
              // or, for that matter, is there a way I could call dimensions.cmp()?


            },

            rect2 : {
                // things go here
            }

        }

    }

};

 

My question is: Is to possible to get the value of dimensions.x from within the dimensions.definition.rect1 function?


Stop using object literals when you want this sort of thing:

var obj = new myConstructor();


function myConstructor(){
    var dimensions = {

        x : 100,
        y : 100,
        w : 300,
        h : 400,
        cmp : function () {

            return this.x + this.y;

        }

    };

    this.definition = {

        base : {

            rect1 : {

              // how do I get the value of dimensions.x ??
              // or, for that matter, is there a way I could call dimensions.cmp()?

              //Dimensions is like a private var now - in scope for the object.

              dimensions.cmp(); //that's how you'd call it.
            },

            rect2 : {
                // things go here
            }

        }

    }

}

Try to think of JS objects created from a constructor as simply functions that forgot to close and garbage collect. All the local vars hang out and are accessible to the public methods you define inside the constructor. Externally defined methods, whether prototyped or just tacked on with . would not have access to dimensions, however. When you do it this way, think of var as private and this. as public. But your public methods defined inside the function can access the var stuff.


You can't really do that dynamically, the this value inside a function will point to the object there the function is bound as a property.

For example, if you invoke foo.bar.func();, the this value inside func will refer to the foo.bar object, there is no way to determine dynamically if this belongs to foo, without explicitly checking all properties of foo (and of course knowing beforehand where to check).

Remember that objects are handled as references, therefore an object can be referenced from many places.

Don't complicate yourself, find your members using the obj reference, keep it simple:

var obj = {
  dimensions : {
    x : 100,
    y : 100,
    //...
    cmp : function () {
      return this.x + this.y;
    }
  },
  definition : {
    base : {
      rect1 : {
       foo: function () {
        obj.dimensions.cmp();
        alert(obj.dimensions.x);
        }
      }
      //...
    }
  }
};


I'm not really sure what you are trying to accomplish, but if you are trying to make multiple instances of this type of object, you could try something like this:

function Structure() {
  this.dimensions = {
    x: 100,
    y: 100,
    w: 300,
    h: 400
  };
  function Rect(dimensions) {
    this.dimensions = dimensions;
    this.func = function() {
      alert(dimensions.x);
    }
  }
  this.definition = {
    base: {}
  };
  this.addRect = function(name) {
    this.definition.base[name] = new Rect(this.dimensions);
  };
};

var obj = new Structure();
obj.addRect('rect1');
obj.addRect('rect2');
obj.definition.base.rect1.func();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜