Javascript Learning Guide 1---0

Source: Internet
Author: User
Tags getcolor

Javascript Learning Guide 1---0
Chapter 2 variables and scopes I hope you can review the previous content if you forget it.
Today, let's talk about the variables and scopes.
This chapter describes the basic types and reference types of execution environment garbage collection (you can understand). The basic types and reference types of JavaScript may contain two different data types:Basic and reference typesA basic type value refers to a simple data segment, and a reference type refers to objects that may consist of multiple values.
How do I define basic type values and reference type values? See the following:
Create a variable and assign a value to it. For the reference type, we can add and delete its attributes and methods.

Var csdn = new Object (); csdn. blog = "Tomihaohao"; alert (csdn. blog) // Tomihaohaovar name = "csdn"; name. age = 123; alert (name. age) // undefined // Why is the result of the same code segment different?

In JS, dynamic attributes can only be added to values of reference types.
We are looking at a piece of code.
Var a = 1; var B = a; alert (B); // ------------------------------------- var obj1 = new Object (); var obj2 = obj1; obj1.name = "csdn "; alert (obj2.name) // "csdn" // do you know what happened to the above two pieces of code in the parser?

The two figures help you explain clearly


Passing parameters: Do you still remember the function parameters described in the previous chapter? Let's review the function parameters in JS. Are you sure you want to remember arguments? If you forget to click here
Today, I want to talk about parameters in JS.
Remember In JS, function parameters are passed by value. Do you still remember the image I drew above? Yes. Copy the external value of the function to the internal parameter of the function, just like copying the value from one variable to another.
Function addnum (param) {num + = 10; return num;} var a = 10; var B = addnum (a); alert (); // 10 alert (B); // 20 // check whether the two of them do not affect each other

Some people may wonder if the reference type is still like this?
Function setObj (obj) {obj. name = "csdn"; obj = new Object (); obj. name = "Tomihaohao"} var T = new Object (); setObj (T); alert (T. name); // What is it? Yes, it's still csdn. // actually, this local object is killed after the function is executed.

New Tool instanceof
Remember that the previous chapter introduced a type of something pair, which is the typeof tool.
But what if you encounter a reference type? Because typeof returns all objects.
The specific usage of instanceof is also very simple.
Var person = [] alert (person instanceof Array) // is person an Array object? Of course!

The execution environment and scope are the most important concepts in javascript: execution environment! The execution environment defines other data that variables and functions have access, it determines their respective behaviors. Each execution environment has a variable object associated with it. In the browser, the window object is a global execution environment, they can destroy each function only when the application exits and each function has its own execution environment code for execution in one environment, A scope identifier of the object variable will be created. Parsing is a process of searching for identifiers at the scope level.
Var color = "white"; function changeColor () {var anotherColor = "red"; function swapColors () {var tempColor = anotherColor; anotherColor = color; color = tempColor; // here you can access color anotherColor tempColor} swaoColors (); // here you can access color anotherColor} changeColor (); // only access color

The above code has three execution environments: The Global changeColor () Local Environment swapColors () local environment. The global environment has a variable color while the changeColor () there is the anotherColor variable and the swapColors () function, and there is a tempColor in swapColors (). Why is it inaccessible in some places?
The scope chain means that the internal environment can access all external environments, but in turn it does not work. Each environment can search for the scope chain to query variables and functions, but they cannot search down.
Remember, JS does not have block-level scope.
Javascript and c java c # are different. They do not have their own block-level scopes. Of course, you can simulate them using some methods. We will discuss this later.
for(var i =0 ;i<10;i++){      console.log(i);}alert(i);  //i=10

If it is JAVA, the variable I will be destroyed immediately, but in JS! Still exist
Query identifier in js
Var color = "blue"; function getColor () {// var volor = "red"; return color;} alert (getColor ()) // blue // If the annotation in getColor () is removed, red is returned.

Yes. In js, identifiers are searched online step by step along the scope until they are found.
GC garbage collection in the browser. In fact, you only need to know a concept, that is, release the reference.
Once the data is no longer useful, it is best to set it to null to release its reference
// Var a = "csdn"; // useless. a = null // unreference

To sum up the specific process of copying data from one variable to another, a copy of the value will be created to reference the type of the value as an object, the variable stored in heap memory that references the type value is actually a pointer reference type copy. In fact, it copies a pointer. They also point to the same object. The new tool instanceof learns the scope chain. understand GC


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.