Basic knowledge of some core JavaScript

Source: Internet
Author: User

First, SetTimeout

Settimtout (function() {   alert (2);                           //  },0); alert (1);                              // pop up first

For the above code, the inclusion principle is as follows:

1, first JSVM will only execute a thread;

2, when this thread encounters settimout, it will say this function put into a queue;

3, when the current thread is idle, the task queue polling code is executed, the function that satisfies the condition is taken out to execute.

A relatively simple typical application scenario is:

$ (elem). HTML (XXXXXX); SetTimeout (function() {  //  Internal DOM operation is complex, The settimeout is used here to ensure that subsequent code 0 is executed after the internal DOM operation is complete and the associated memory is freed  ;

Second, eval

In addition to the global scope and function scope, there is an eval scope in JS.

When the Eval function executes, a scope is created based on the current execution context.

Here is the following code:

function A () {  varnew  SomeThing ();   return function () {    eval (');  };}

If the internal anonymous function uses eval, even if the code executed in eval does not use the B variable, B will not be freed, and if the Eval function is not used, some browsers will release B, and some will choose not to release B based on their own optimizations.

So, Eval is usually not supposed to be executed under a non-global scope, so jquery has the Globaleval function.

Third, pre-compilation

The code for each of the following fragments is first:

<HTML><Head><Script>a ();functionA () {alert (1);}</Script></Head><Body></Body></HTML>

<HTML><Head><Script>a ();</Script><Script>functionA () {alert (1);}</Script></Head><Body></Body></HTML>
<HTML><Head><Script>a ();varb= functionA () {alert (1);}</Script></Head><Body></Body></HTML>

The results of the execution were:

The first paragraph of code: Popup 1;

The second paragraph of code: the first script fragment error, a does not exist;

The third paragraph of code: error, a does not exist;

Reason:

In the first piece of code, JSVM compiles and constructs a function, so the order of access to a function is unimportant;

In the second code, precompilation is performed in chunks of code, and each script will form a block of code, even if the script introduces JS through SRC;

In the third code, the definition of a function is placed in an expression, so JSVM is not precompiled.

However, you can continue to drill down here, and the third code is changed to look like this:

<HTML><Head><Script>varb= functionA () {alert (1);} A ();</Script></Head><Body></Body></HTML>

This code will also error, a does not exist. Why is it? The reason is that the a function is placed in the expression, jsvm a function as an anonymous function, so after the current statement execution, A is released.

However, please note that the above discussion is based on the results of the standard, in the third and fourth paragraphs of code, IE 6, 7, 8 or will be precompiled, and will not release a, so no error.

Iv. self-executing functions

Several forms of self-executing functions:

(function() {}) ();( function () {}());! function (){}(); void function (){}();

The third way to do this is to create additional operations, because the returned content is "not".

V. Weak conversions [TODO]

Vi. variable declarations in precompilation

The following code:

function A (x) {  return x*2;} var A;alert (a);

var a just declares that it does not do anything, var A will only let the current scope of alert (a) Not report A does not exist error, A is actually what, need to display assignment, if Var a already exists before, then do nothing.

Vii. Function-Transfer parameters

The following code:

function Fn1 (A, B, c) {   = 1.2;    = 2.2;    = 3.2;} var a = 1; var b = 2; var c = {c:3};fn1 (a,b,c); alert (a); alert (b); alert (C.C);

The principle is not difficult to understand, note the difference between a value type and a reference type.

Viii. Scope Chain

There are several points:

1, the scope chain is defined at the time of the determination down;

2. Implicit object model: For a scope, JSVM creates an implicit object and then binds the various variables of the current scope on top of the object. Nested definitions of multiple functions also form a scope chain. If you want to access a variable in a scope, you will first look for a variable in the current scope, or if it does not exist, find out if there is a variable in the parent scope implicit object, and so on, and if you cannot find the A variable at the root scope, you will get an error.

Nine, class array structure

First question, how to construct the class array structure? The idea is simple, do not repeat.

The jquery selector constructs a class array structure.

Ten, the factory model

function Factory () {  returnnew  thisisaclass ();}

Advantages:

1, can prevent the caller to the constructor directly as a function call;

2. Omitting the new keyword into a piece, jquery is a positive example.

Basic knowledge of some core JavaScript

Related Article

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.