[JS] JavaScript Beginner (3) Advanced

Source: Internet
Author: User

This section will focus on the following points.

Knowledge points: Multithreading, scopes, closures, this

Look after the top

1. Multithreading

In browsers that do not support H5. Use Concurrent.Thread.js.

In support H5, use webwork.

Before H5, JS is not support multi-threaded. There is the concept of a synchronous pool and an asynchronous pool. When the synchronization pool finishes processing, the asynchronous pool's events, scheduled tasks, and so on are invoked.

The following is a clear distinction between whether JS is multi-threaded execution

C#

Js

    <script>        setinterval (function () {            alert (' 1 ');        }, +);    </script>

Use Concurrent.Thread.js (essentially JS timer function to simulate multithreading)

    <script>        Concurrent.Thread.create (function () {            $ (BTN). Click (func);            function func () {                alert (' message ');            }            var i = 0;            while (1) {                console.log (i);            }        })    </script>

  

Use Webworker (also cannot manipulate DOM elements)

    <button id= "btn" >Test</button>    <script>        var work = new Worker (' task.js ');        Work.onmessage = function (event) {         //callback function            alert (event.data);        }        Work.postmessage ();                         Call work, you can pass the argument        $ (BTN). Click (func);        function func () {            alert (' message ');        }    </script>task.js:onmessage = func; Called Method function func () {    var i = 0;    while (1) {        console.log (i++);    }}

  

2. Scope

JS is a function-level scope with internal access to external variables. No block-level scope (if else try catch)

    <script>        if (false) var j = +;         1. Function-level scope  2. Variable Front        alert (j);                       Pop-up undefined        alert (i);                       Error not defined    </script>

The self-executing function executes after declaring the variable.

    <script>        var j = +;        +function func () {          //Here's ~, can also be replaced with +,!, ~ and other unary operator            alert (j);               Eject undefined            var J;                  Variable Front        } ();                        Without this, the parser will assume that function is the beginning of a functional declaration, and the latter () will result in a syntax error. When the function is preceded by a ~, it becomes a functional expression, and a function expression is added after another () becomes a function that executes immediately.    </script>

 

 

3. Closures

Closures are changing the scope of variables. Closures can create memory problems that cannot be released.

        function func () {            var j = Ten;                 Functions outside the function cannot be accessed to J            return function () {        //by means of closures to allow access to J                return J;            }        }        var k = func () ();        Alert (k);

 

 

4.this

Who calls, this refers to WHO. When a closure exists, be aware of the caller this is pointing to.

    <script>        this.m = ten;        var obj = {            m:1            , Func:function () {                               alert (THIS.M);            }            , Bb:function () {                 //closure function                return func tion () {                    alert (THIS.M);}}        }        Obj.func ();                             This refers to obj        obj.bb () ();                             End this means window    </script>

  

Accessories: Concurrent.Thread.js

This section is also just a call, the content may be wrong, I hope that if you find a mistake, please timely inform.

[JS] JavaScript Beginner (3) Advanced

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.