考虑到javascript没有主要方法,执行的顺序是什么? [英] Considering javascript has no main method, what is the order of execution?

查看:68
本文介绍了考虑到javascript没有主要方法,执行的顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *所以这是一个产生输出的程序:



//→___ /''''\ ______ /'\ _



* /



/*So this is a program that produces the output:

// → ___ /''''\ ______ /'\_

*/

//Function 1
var landscape = function () {
    var result = "";

    var flat = function ( size ) {  //function 2
        for (var count = 0; count < size ; count ++)
            result += "_";
    };

    var mountain = function ( size ) {   //function 3
        result += "/";
    
        for (var count = 0; count < size ; count ++)
            result += " '";
    
        result += "\\";
    };
    
    flat (3);
    mountain (4);
    flat (6);
    mountain (1);
    flat (1);
    return result ;
};

console.log(landscape());





我尝试了什么:



我猜它是从console.log开始的......

但不确定下一步...



What I have tried:

I'm guessing it starts at console.log...
but not sure what next...

推荐答案

通常自上而下,但也基于你如何创建函数。示例声明函数可以放在任何位置,而表达式必须放在顶部
Top down usually but also based on how you create function. example Declaration function can put anywhere while expression must put on to top


请参阅我对解决方案1的评论。



这是不那么容易解释。处理分为两个阶段,一切都是从上到下。



首先,类似于一些词法分析。如果存在词汇错误,则整个脚本会无声地失败。在此阶段,函数声明对于脚本是已知的,因此它们可以在调用函数时查看代码。然后执行阶段从上到下开始。



这是一个微妙的时刻。函数对象的用法取决于它的声明方式。这是我的例子:

Please see my comment to Solution 1.

This is not so easy to explain. The processing goes in two phase, and everything goes from top to bottom.

First, something like some lexical analyses goes. If there are lexical errors, whole script fails silently. On this phase, function declarations becomes known to the script, so they can go after the code when the functions are called. Then the execution phase starts from top to bottom.

This is a delicate moment. The usage of a function object depends on how it is declared. This is my illustration:
try {
   b(); // surprisingly, will work
   a(); // will throw exception
} catch(e) { alert(e); }

var a = function() { alert("a!"); };
function b() { alert("b!"); }



在这个例子中, b 声明了一个函数,它将在第一次传递时被处理并被考虑在内执行阶段。与函数对象的赋值相反,语句,在调用时不执行一个(a)中。在调用时, a 将等于对象 undefined ,并引发异常。 />


注意在语句之后使用';'而在函数定义之后缺少它;实际上,这是赋值运算符之后的终止符(赋值给对象 a )。实际上,在这两种情况下都可以丢失或添加;但我建议使用它与我所展示的方式完全一样;如果您在一条线路上使用多个语句,以及其他一些情况,将会显示检测差异。



如果您显示的代码也值得注意在字符串上方并将其传递给 eval b 将无效,因为词汇阶段将不会执行关于字符串内容。规则是相同的,但有一个微妙的有效差异。



最后,关于 main 的说明。看起来你对它有一个非常普遍的误解。这不是任何语言的基本特征,甚至不是C或C类。这是关于语言和运行时环境之间边界的约定。即使使用C / C ++,你也会发现类似WinMain之类的结构,等等(不要告诉我这只是隐藏真实 main ;这是一个纯粹的惯例)。在许多其他语言中,没有这样的惯例;它根本不是编程的基础。



-SA


In this example, b declared a function, it will be processed on first pass and taken into account in the execution phase. In contrast to that assignment to the function object a is a statement, which is not executed at the moment of the call a(a). At the moment of the call, a will be equal to the object undefined, and that throws an exception.

Note the use of ';' after statement and lack of it after the function definition; essentially, this is a terminator after the assignment operator (assignment to the object a). In fact, ';' can be missing or added in both cases, but I recommend to use it exactly the way I've shown; the detecting difference will be revealed if you are using more than one statement on a line, and some other cases.

It also worth noting that if you make the code shown above a string and pass it to eval, b won't work, because the lexical phase won't be performed on the string content. The rules are the same, but there is a subtle effective difference.

And finally, a note on main. It looks like you have a pretty common misconception about it. This is not a fundamental feature of any language, not even C or C-like. This is a convention on the boundary between the language and the runtime environment. Even with C/C++, you will find similar constructs like "WinMain", and so on (don't tell me that is simply hides "real" main; this is a pure convention). In many other languages, there is no such convention; it is not fundamental in programming at all.

—SA


我回答了标题解决方案2中的问题,但我忽略了代码示例中的未写问题。



如果这是完整代码,则不会发生任何事情。你只需创建一个函数对象,但不要调用它。



有一个众所周知的模式只能调用一次函数:

I answered to the title of the question in Solution 2, but I ignored the unwritten question in the code sample.

If this is the complete code, nothing will happen. You simply create a function object, but don't call it.

There is a well-known pattern for calling some function just once:
(function landscape() {
   alert("working");
   // ...
})();



现在,请注意名称 landscape 永远不会被使用;你可以删除它;该函数可以是匿名

更广泛的模式可以有几种不同的形式;它被称为 IIFE

立即调用的函数表达式 - 维基百科,免费百科全书 [ ^ ]。



请学习;它非常实用。



-SA


Now, note that the name landscape is never used; you can just delete it; the function can be anonymous.
The wider pattern can have several different forms; it's called IIFE:
Immediately-invoked function expression — Wikipedia, the free encyclopedia[^].

Please learn it; it's very useful.

—SA


这篇关于考虑到javascript没有主要方法,执行的顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆