简体   繁体   English

如何在JavaScript中定义函数的原型?

[英]How can I define a function's prototype in JavaScript?

How can I define a function's prototype in JavaScript? 如何在JavaScript中定义函数的原型? I want to do this like I would do it in C where you would do something like: 我想像在C语言中那样做,您将像这样:

void myfunction(void);

void myfunction(void){
}

So is there any way to do this in JavaScript? 那么有没有办法用JavaScript做到这一点? I'm asking this because of the function declaration order that is required in JavaScript. 我之所以这样问是因为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. privateFunction需要访问publicFunction声明的publicFunction

How can I do this? 我怎样才能做到这一点?

JavaScript is dynamic language there is no need to do that. JavaScript是动态语言,因此无需这样做。 Meaning Javascript has dynamic binding and dynamic typing, so the check will be on the run time. 意味着Javascript具有动态绑定和动态类型,因此检查将在运行时进行。 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. 但是,假设您在出现问题后提出了自己的问题,那么您可能会解决JavaScript范围界定的问题。

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. 您的问题与C上的“定义与声明”无关,而是与语句顺序有关。

in JavaScript (as with most dynamic languages) a function definition like this 在JavaScript中(与大多数动态语言一样),这样的函数定义

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. 你可以看到, whatever是实际上是一个变量,它分配一个函数值。 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 (...) {
   ...
}

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/ 更多信息和示例: http : //www.wait-till-i.com/2007/08/22/again-with-the-module-pattern-reveal-something-to-the-world/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM