3

Why I cannot do this?

var MyObject = {}
MyObject.foo = function(){
  this.sayhello = function(){
    alert('Hello');
  }
}
MyObject.foo.sayhello();

Any ideas on how it could be done?

xavip
  • 545
  • 5
  • 14
  • possible duplicate of [Javascript Function-Pointer Assignment](http://stackoverflow.com/questions/2326072/javascript-function-pointer-assignment) – atk Feb 06 '13 at 18:47
  • http://stackoverflow.com/questions/2326072/javascript-function-pointer-assignment – atk Feb 06 '13 at 18:47
  • 1
    @atk, I don't believe that linked question is related to what's being asked here. – zzzzBov Feb 06 '13 at 18:52
  • @zzzBov: You're right, it's not exactly the answer, but it gives part of the answer - that is, code that works (where the question appears to ask because their code doesn't work). – atk Feb 08 '13 at 04:13

4 Answers4

6

within foo, this references MyObject, which means that after:

MyObject.foo();

you can call:

MyObject.sayhello();

If you want to be able to call MyObject.foo.sayhello(), you need sayhello to be a function on MyObject.foo:

var MyObject = {}
MyObject.foo = function () {...};
MyObject.foo.sayhello = function () {
    alert('hello');
}

If you don't need foo to also be a function, you could simply declare:

var MyObject = {
    foo: {
        sayhello: function () {
            alert('Hello');
        }
    }
}

which would allow you to call:

MyObject.foo.sayhello();
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • this is ok, but it's not mapped by netbeans navigator..., I have to use commas between functions – xavip Feb 06 '13 at 19:00
  • I should also mention that within the `sayhello` function, `this` will reference `MyObject.foo` not `MyObject`. – zzzzBov Feb 06 '13 at 19:02
1

You have to call MyObject.foo() first so that the this.sayhello function actually gets added. Then you should beable to call MyObject.foo.sayhello();

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 2
    This is wrong, you should be able to call `MyObject.sayhello()` not `MyObject.foo.sayhello()` because `this` references `MyObject`. – zzzzBov Feb 06 '13 at 18:48
0
var MyObject = {}
MyObject.foo = {
  sayhello: function(){
    alert('Hello');
  }
}
MyObject.foo.sayhello();
bevacqua
  • 47,502
  • 56
  • 171
  • 285
0

That is because the function does not yet exist. Therefore you must call foo first. What you do is calling the function sayhello from the property foo. But you don't have a property foo. You have a function foo.

But you can also do this and make it chain, like jQuery does:

var MyObject = {}
MyObject.foo = function(){
  this.sayhello = function(){
    alert('Hello');
  }
  return this;
}
MyObject.foo().sayhello();

Make a function sayhello inside object foo, so not a chained function. But an object inside an object with a function.

var MyObject = {
    foo : {
        sayhello : function(){
            alert("hello");
        }
    }
}
MyObject.foo.sayhello(); // Now does work!
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Niels
  • 48,601
  • 4
  • 62
  • 81
  • Yes but this means I have to use commas between methods, so netbeans navigator is not mapping the methods... – xavip Feb 06 '13 at 19:02