Home > Web Front-end > JS Tutorial > body text

What does pre-interpretation of js mean? A brief analysis of the meaning of pre-interpretation in js

不言
Release: 2018-08-22 17:59:50
Original
1615 people have browsed it

What this article brings to you is what does the pre-interpretation of js mean? A brief analysis of the meaning of pre-interpretation in js has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

I often heard pre-explanation and pre-explanation before, so what exactly is pre-explanation? Let’s talk briefly about it today.
Pre-interpretation is also what we usually call variable declaration. Before the js code is executed in the current scope, the browser will first declare or define all the var and function in advance by default. This is It can be called pre-interpretation.
So what is a statement and what is a definition?
Declaration: For example,

    var test
Copy after login

Then this is telling the browser that we have declared a variable called test in the global scope.

Definition: For example,

    test=2
Copy after login

gives The variables we declare are assigned values

For those with the var and function keywords, they are also different in the pre-interpretation. Those with var are only declared in advance in the pre-interpretation, while function must be declared and defined in the pre-interpretation. This is why we said above that "declare or define in advance"
For example Example

    var test = 2
    var obj = {"name":"代码"}
    function foo (val) {
        var test2 = val
        console.log(test2)
    }
Copy after login

The above code is pre-interpreted when executed from top to bottom in the global scope. First, declare a test, declare an obj, and then declare and define a foo. So will var test2 in foo be pre-interpreted in the global scope? Of course it won't work. We also said above that "js is executed in current scope" test2 is in the scope of foo, and it will only be pre-interpreted when foo is executed. Therefore, pre-interpretation only occurs in the current scope, and only when the function is executed will it be pre-interpreted.

So we may encounter some such questions when we go out for interviews

    console.log(test )
    foo(5)
    var test = 2
    console.log(test )
    var obj = {"name":"代码"}
    function foo (val) {
        var test2 = val
        console.log(test2)
    }
Copy after login

So what is the printed content?

    console.log(test )//undefined
    foo(5)//5
    var test = 2
    console.log(test )//2
    var obj = {"name":"代码"}
    function foo (val) {
        var test2 = val
        console.log(test2)
    }
Copy after login

Because at the beginning, test was only declared, but it was not defined, so the first console was undefind, but then when the second console came, 2 had been assigned to test, so the second console has value. And foo is a function. During pre-interpretation, it must be declared and defined, so foo(5) has value.

Related recommendations:

Pai Pa Pa JavaScript pre-explanation_javascript skills

Detailed explanation of conditional comments in JScript_javascript Skill


The above is the detailed content of What does pre-interpretation of js mean? A brief analysis of the meaning of pre-interpretation in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!