2

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?

4
  • possible duplicate of Javascript Function-Pointer Assignment Commented Feb 6, 2013 at 18:47
  • stackoverflow.com/questions/2326072/… Commented Feb 6, 2013 at 18:47
  • 1
    @atk, I don't believe that linked question is related to what's being asked here. Commented Feb 6, 2013 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). Commented Feb 8, 2013 at 4:13

4 Answers 4

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();
Sign up to request clarification or add additional context in comments.

2 Comments

this is ok, but it's not mapped by netbeans navigator..., I have to use commas between functions
I should also mention that within the sayhello function, this will reference MyObject.foo not MyObject.
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();

1 Comment

This is wrong, you should be able to call MyObject.sayhello() not MyObject.foo.sayhello() because this references MyObject.
0
var MyObject = {}
MyObject.foo = {
  sayhello: function(){
    alert('Hello');
  }
}
MyObject.foo.sayhello();

Comments

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!

1 Comment

Yes but this means I have to use commas between methods, so netbeans navigator is not mapping the methods...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.