1

If I have

var function_holder = { 

        someFunction: function () { } 
}; 

How do I call someFunction.

Also is there a way to write someFunction outside of function_holder?

Thanks

1
  • function_holder.someFunction(); I don't understand your second question Commented Nov 8, 2011 at 1:18

5 Answers 5

2

function_holder is an object, someFunction is a method.

Call function_holder.someFunction(); to invoke the method.

You can define the function seperately as either a function declaration

function someFunction() {
    // code
} 

or a function variable

var someFunction = function () {
    // code
}
Sign up to request clarification or add additional context in comments.

Comments

0

Call it:

function_holder.someFunction()

From outside:

function other() {
    alert('Outside');
}

var obj = {
    someFunction: other
};

obj.someFunction(); // alerts Outside;

Comments

0

You could write this instead:

function_holder.someFunction = function() { }

You can call the function this way:

function_holder.someFunction()

Comments

0

call it:
function_holder.someFunction() This will execute the function like the others say.

I think I have an idea what you mean by "write someFunction outside of function_holder?"..

var theFunc = function(){};
function_holder = {};
function_holder.someFunction = theFunc;

There are lots of ways to do anything in javascript.

Comments

0

There are a couple different ways to do what you want to do.

function function_holder() {

    this.my_function = function() {};
};

You would call this by instantiating function holder as such.

var fc = new function_holder();
fc.my_function();

Or alternatively you could do it this way.

function function_holder() {};

function_holder.prototype.my_function = function() {};

You would call this the following way:

function_holder.my_function();

1 Comment

That's wrong. function_holder is an object and new <Object> throws a type error

Your Answer

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