Home  >  Article  >  Web Front-end  >  Xiaoqiang’s HTML5 mobile development road (29) - JavaScript review 4

Xiaoqiang’s HTML5 mobile development road (29) - JavaScript review 4

黄舟
黄舟Original
2017-02-04 14:22:451218browse

1. Scope of variables

The execution process of JavaScript script is divided into two stages:

In the first stage, the js engine () first scans the entire javascript code. When encountering 3f1c4e4b6b16bbbd69b2ee476dc4f83a, a global active object will be created first, and the declaration of variables and function definitions appearing in 3f1c4e4b6b16bbbd69b2ee476dc4f83a will be saved in the active object. If a function is encountered, a corresponding local active object is created, and the declaration of variables inside the function and the definition of the function are saved in the active object.

In the second stage, when executing the javascript code, when encountering a variable, it will first search from the corresponding active object. If it cannot find it, it will find the upper-level active object.

<html>  
    <head>      
        <script>  
            var i = 100;  
            function f1(){  
                alert(i);  
                var i = 1000;  
            }  
            function f2(){  
                var i = 1000;  
                alert(i);  
            }  
            function f3(){  
                alert(i);  
                i = 1000;  
            }  
            function f4(){  
                alert(i);  
                i = 1000;  
                function f5(){  
                    var i = 10000;  
                }  
            }  
            f1(); //结果是undefined  
            f2(); //结果是1000  
            f3(); //结果是100  
        </script>  
    </head>  
    <body>  
  
    </body>  
    <!--  
        预编译--对脚本扫描  
        js引擎--创建一个全局活动对象(i和f1)--创建一个局部的活动对象(i)s  
    -->  
</html>

2. Math (an object built into javascript, which can be used directly)

Math.random(): Returns a random number between 0 and 1 0<= random number<1
Math.ceil(): Round up

Math.floor(): Round down

<html>  
    <head>  
        <script>  
            function f1(){  
                var a1 = Math.random();  
                alert(a1);  
                var a2 = 100.68;  
                alert(Math.ceil(a2));  
                alert(Math.floor(a2));   
            }  
            function f2(){  
                Math.floor(Math.random()*33);  
            }  
            f1();  
        </script>  
    </head>  
    <body>  
    </body>  
<html>

3. W3C DOM model

1. What is dom

document object model

Converts a structured document (xml, html) into a tree and provides operations on the tree (including traversal, search, modification , delete, etc.) related attributes or methods

2. The basic structure of w3c dom model

Node
DocumentHTMLDocument  
(<html>)HTMLBodyElement  
(<body>)ElementHTMLElementHTMLFormElement  
(<form>)HTMLInputElement  
(<input>)HTMLSelectElement 
(<select>)HTMLOptionElement  
(<option>)

The above is the content of Xiaoqiang’s HTML5 mobile development road (29) - JavaScript review 4, For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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