JS Advanced Programming Fourth Chapter Summary

Source: Internet
Author: User

variables, scope, and memory issues

The ES variable contains values, base types, and reference types for two different data types.

There are five basic types: Undefined, Null, Boolean, Number, String. A reference type is an object that is saved in memory.

There are some differences when we access the basic types and reference types.

1.1.1 Copy variable values

var num1 = 5;

var num2 = NUM1;

In memory, NUM1 and num2 are completely independent, and modifying the value of either variable will not affect the other. Because 5 is a primitive type value, when you copy the value of a primitive type from one variable to another, a new value is created directly on the variable object, and the value is assigned to the location assigned to the new variable.

But when you copy the value of a reference type from another variable to another variable, at the end of the copy, you actually create a new pointer to the object, so two variables actually refer to the same object.

var obj1 = new Object ();

var obj2 = obj1;

Obj1.name = "Nich";

Console.log (Obj2.name); "Nich"

1.1.2 Passing parameters

All functions in Es are passed by value.

The function's arguments are passed to the variables inside the function first, and then the variables are manipulated.

It can be understood that if we first declare a function, we need a parameter, and then do some processing on the variable:

function Add1 (num) {

num++;

}

var a = 5;

ADD1 (5); A = 5;

After this function is executed, the value of a is still 5.

When you pass a as a parameter into a function, you can think of what is happening internally:

function Add1 (num) {

num = A;

num++;

}

Since a is a basic type, when num = A is executed, it is equivalent to creating a new value directly, and subsequent operations have no effect on a.

It's also understandable what happens when a function is passed in to an object.

function AddName (obj) {

Obj.name = "Nich";

}

var myObj = {};

AddName (MYOBJ); Myobj.name = "Nich";

Looks like the function affects the external value, the function inside still occurs similar to the above process, when the incoming MYOBJ, the internal equivalent of such processing

function AddName (obj) {

obj = MYOBJ;

Obj.name = "Nich";

}

But remember that when you try to copy an object by using obj = MYOBJ, you actually get an object reference. The operation of an object by any one variable is immediately reflected in the other variable. Therefore, this does not mean that the functions in ES are passed by reference.

1.1.3 Detection Type

Here's a look at the TypeOf and instanceof operators

Previously said TypeOf has six return values, which are ' undefined ', ' Boolean ', ' string ', ' number ', ' object ', ' function '

typeof is simple when determining whether a variable is a string, a value, a Boolean, or a undefined, but an object is an object or null, and it returns object

When we want to know what type of object a value is, you can use the instanceof operator, which will determine if a variable is an instance of a given reference type, and returns True or false.

In fact Object.prototype.toString.call () is a good judge of which data type a variable is.

var number = 5;
var string = "Hello";
var nul = null;
var notdefined;
var obj = {};
var boo = true;

Console.log (Object.prototype.toString.call (number)
, Object.prototype.toString.call (String)
, Object.prototype.toString.call (NUL)
, Object.prototype.toString.call (notdefined)
, Object.prototype.toString.call (obj)
, Object.prototype.toString.call (boo));
[Object number] [object String] [object Null] [object Undefined] [Object Object] [object Boolean]

Execution Environment and scope

Execution Environment: The execution environment defines other data that a variable or function has access to. Each environment has a variable object associated with it, and all variables and functions defined in the environment are stored in the object.

When code executes in an environment, a scope chain of object variables is created to guarantee orderly access to all variables and functions that the execution environment has access to.

The front end of a scope chain is always a variable object for the environment in which the code is currently executing, and the next variable object comes from the containing environment (the external environment), and the next variable from the next containment environment, continuing to the global execution environment.

Identifiers are the process of searching identifiers at a level along the scope chain.

Each environment can search up the scope chain, query for variables and functions, but no environment will be able to enter another execution environment by searching for the domain chain that is used.

2.1.1 does not have a block level scope

Block-level scope: In some languages, blocks of code that are enclosed by curly braces have their own scopes (i.e. their own execution environment), and the variables defined inside them cannot be accessed out of the scope.

in Es, however, the variables defined in the IF statement block are still accessible after the curly braces. This is caused by the function scope attribute of the Var declarator .

if (true) {
var color = "Red";
}

Console.log (color); Red

Function name () {
var a = "Nich";
}

Console.log (a); Referenceerror

The addition of the const and let Declarators in ES6, which provides a block-level scope, avoids many bugs caused by var. Best practice: Use const to declare variables in any case, unless you must make modifications to the variables, use the LET operator to declare the variables.

Both const and let will cause an error in the first case above.

Query identifier:

When an environment references an identifier for reading or writing, it must be searched to determine what the identifier actually represents.

The search process starts at the front of the scope chain and queries the identifiers that match the given name upwards. If the identifier is found in a local variable, the search process stops. If the variable name is not found in the local variable, it is searched up the scope chain and goes back to the variable object of the global environment. If the global variable does not find the identifier, it means that the variable is not declared (or is not in the range that it can access).

JS Advanced Programming Fourth Chapter Summary

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.