0

I have the following object:

​var x = {
 y: '33', 
 z: '88', 
 m: function() {
 alert('this is the m element');
 }
};
x.newObject = function() {
 alert('mm'); 
 function click(myval) {
 alert('you have select the click function' + myval); 
 }
};
x.newObject.click('clickme'); ​​​​​​​​​// This is what I have tried; it's not working. 

How can I call the click function of newObject?

jsFiddle

asked Jul 9, 2012 at 20:22

5 Answers 5

5

In the way you are coding it, you can't access click function since it's within another function.

If you insist coding it this way, have a call to newObject (which is a function) return click in an object so that it can be accessible. It's usually called function call "chaining", where the previous call returns an object that the next call can then use:

x.newObject = function(){
 alert('monmon') ; 
 return {
 click : function (myval){
 alert('you have select the click function' + myval) ; 
 }
 }
}
//a call to newObject returns an object with a click function
x.newObject().click('clickme')
answered Jul 9, 2012 at 20:26
Sign up to request clarification or add additional context in comments.

Comments

1

This worked:

var x = {
 y: '33',
 z: '88',
 m: function() {
 alert('this is the m element');
 }
}
x.newObject = new function() {
 return {
 click: function(myval) {
 alert('you have select the click function' + myval);
 }
 }
}
x.newObject.click('clickme');​
answered Jul 9, 2012 at 20:27

Comments

1

click is scoped inside newObject, so it's not available anywhere outside of newObject. Are you trying to create an object with methods? If so, add it to the prototype:

newObject = function() { ... };
newObject.prototype = {
 click: function(myval){ .. }
};
//now you can call newObject.click()

or you can have newObject return the methods you want to expose:

newObject = function(){
 return {
 click: function(myval){ ...}
 }
};
answered Jul 9, 2012 at 20:28

Comments

0

Short answer: no way. Long answer: 'click' is treated somewhat like a local variable inside your bigger function, you cannot access local variables outside of function where they were declared. You need to assign your 'click' function to some globally accessed property or variable.

answered Jul 9, 2012 at 20:27

Comments

0

Maybe newObject shouldn't be a function, but an object. The following code works for me

var x = {
 y: '33',
 z: '88',
 m: function() {
 alert('this is the m element');
 }
}
x.newObject = {
 click: function(myval) {
 alert('you have select the click function' + myval);
 }
}
x.newObject.click();​
answered Jul 9, 2012 at 20:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.