views:

40

answers:

2

In the example below I'm trying to access the parents sibling. Is there a better way than what I've come up with, what gives?

Broken:

var funkyness = function(){

    var some_obj = {
        foo: function() {
            alert('bar');
        },

        'wrapper' : {
            'OK': function() { 
                // I want to access some_obj.foo
                foo(); // foo is not defined
            }
        }
    }

    some_obj.wrapper.OK();
};

Seems like a hack fix:

var funkyness = function(){
    var afoo;
    var some_obj = {
        foo: function() {
            alert('bar');
        },

        'wrapper' : {
            'OK': function() { 
                // I want to access some_obj.foo
                afoo();
            }
        }
    }

    afoo = some_obj.foo;
    some_obj.wrapper.OK();
};
+1  A: 

Why not use:

var funkyness = function(){

 var some_obj = {
  foo: function() {
   alert('bar');
  },

  'wrapper' : {
   'OK': function() {
     debugger;
    // I want to access some_obj.foo
    some_obj.foo(); // foo is a member of some_obj
   }
  }
 }

 some_obj.wrapper.OK();
};
zincorp
Wow. I feel like an idiot - that had to be one of the first things I tried. I must have typo'ed it or something. Thanks!
Zac
A: 

Why don't you add a prototype?

some_obj.prototype.foo = function() 
{
    // bleh
}
JeremySpouken