0

How can I define a function's prototype in JavaScript? I want to do this like I would do it in C where you would do something like:

void myfunction(void);

void myfunction(void){
}

So is there any way to do this in JavaScript? I'm asking this because of the function declaration order that is required in JavaScript.


Edit:

I have an object:

function something(){
 var anArrayOfSomethingObjects;
 aPrivateFunction(){ 
    // does some stuff
    anArrayOfSomethingObjects[3].aPublicFunction();
 }

 return {
  functionA: function() { },
  aPublicFunction: function() { },
  functionC: function() { }
 }
}

privateFunction needs to access publicFunction which is declared afterwards.

How can I do this?

2
  • I don't see a case where you would ever need this Commented Jul 1, 2009 at 17:04
  • You don't need to declare a function before use in JS. Commented Jul 1, 2009 at 17:10

3 Answers 3

8

JavaScript is dynamic language there is no need to do that. Meaning Javascript has dynamic binding and dynamic typing, so the check will be on the run time. So no need to define forward declaration like in static languages.

Reply to edit:

Even in this case you still do not need to define forward declaration. Once object will have that method or field in run-time it will work fine. But you probably tackled to the problem of scoping in JavaScript, assuming you asking your question after something gone wrong.

Sign up to request clarification or add additional context in comments.

1 Comment

The other interesting bit of information that might help is that anArrayOfSomethingObjects[3].aPublicFunction() and anArrayOfSomethingObjects[3]['aPublicFunction']() are equivalent statements. Whenever you do obj.something, you are actually looking up an attribute with name 'something' in obj's set of attributes.
3

you have to really understand what is a dynamic language, and why your question doesn't really make a lot of sense. your problem is not about 'definition vs. declaration' like on C, it's most about statements order.

in JavaScript (as with most dynamic languages) a function definition like this

function whatever (params) {
   ...
}

is in fact syntactic sugar for an assignment statement like this:

whatever = function (params) {
   ...
}

as you can see, whatever is in fact a variable, and it's assigned a function value. so you can't call it before assigning it.

Of course, execution order doesn't have to follow lexical order. If that's your case, you just have to make sure you assign the needed variable before using it. If it's a local variable and you want closure semantics, you can define it first and assign later, like this:

var myfunc = undefined;
function first () {
   ...
   myfunc (...);
   ...
}
myfunc = function (...) {
   ...
}

2 Comments

but the functions are already assigned within each position of the array.
A function declaration and a function expression are not the same! See yura.thinkweb2.com/named-function-expressions
0

What you are looking for is the Module pattern:

function Something()
{
    var someObjects;
    var pub = {};

    var privateFunction = function() 
                          {
                               pub.publicFunction();
                          }

    pub.functionA = function() {};
    pub.functionB = function() {};
    pub.publicFunction = function() {};

    return pub;
};

More info and examples: http://www.wait-till-i.com/2007/08/22/again-with-the-module-pattern-reveal-something-to-the-world/

Comments

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.