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
-
function_holder.someFunction(); I don't understand your second questiondMb– dMb2011年11月08日 01:18:32 +00:00Commented Nov 8, 2011 at 1:18
5 Answers 5
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
}
Comments
Call it:
function_holder.someFunction()
From outside:
function other() {
alert('Outside');
}
var obj = {
someFunction: other
};
obj.someFunction(); // alerts Outside;
Comments
You could write this instead:
function_holder.someFunction = function() { }
You can call the function this way:
function_holder.someFunction()
Comments
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
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
function_holder is an object and new <Object> throws a type error