JavaScript Small white Study Guide 1---0

Source: Internet
Author: User
Tags getcolor

Chapter II variables and scopes in the second chapter I hope that you can review what you said earlier if you have some forgotten points here
Today, let's talk about variables and scope issues.
Main contents of this chapter
    • Basic types and reference types
    • Execution Environment
    • Garbage collection (Learn about it)
The base type and reference type JS may contain values for two different data types: basic types and reference typesPrimitive type values refer to simple data segments and reference types refer to objects that may consist of multiple values.
How do you define a base type value and a reference type value? Look underneath.
It's OK to create a variable and assign it a value. For reference types we can add and remove properties and methods of it
var csdn = new Object (); csdn.blog = "Tomihaohao"; alert (Csdn.blog)   //Tomihaohaovar name = "Csdn"; name.age = 123;alert ( Name.age)  //undefined//the same way of code why does the result not be the same?

you can only add dynamic properties to the value of a reference type in JS
We're looking at some code.
var a =1;var B = A;alert (b);//-------------------------------------var obj1 = new Object (), var obj2 = Obj1;obj1.name = "CS DN "; alert (Obj2.name)  //" CSDN "//Do you know what happened to the above two pieces of code in the parser?

Two pictures to help you explain.


Pass Parameters:     Remember the arguments in the previous chapter about the function? Let's review the function parameters in JS is very flexible, remember arguments[]? If you have forgotten click here
Today, I'm going to talk about the parameters in JS.
Please remember The parameters of the function in JS are all passed by value.  Do you remember the picture I drew above? Yes, copy the values from the outside of the function to the arguments inside the function, and copy the values from one variable to the other.
function Addnum (param) {   num +=10;   return num;} var a = 10;var B = Addnum (a); alert (a);   10alert (b);   20//look at the two of them that don't affect each other.

Someone might wonder if it's a reference type or something like that?
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.

the new tool instanceof
Remember the previous chapter to introduce you to a type of STH is typeof this tool
But what if you encounter a reference type? Because TypeOf returns an object.
Here to introduce instanceof specific usage is also very simple
var person = []alert (person instanceof Array)//person is an array object? Of course it is!

The execution environment and scope are one of the most important concepts in javascript: the execution Environment!
    • The execution environment defines the other data that variables and functions have access to, and determines their respective behavior
    • Each execution environment has a variable object associated with it
    • The Window object in the browser is the global execution environment, and they can only be destroyed when the app exits
    • Each function has its own execution environment
    • When code executes in an environment, it creates a scope for the object variable
    • Identifier parsing is the process of searching identifiers along the scope level
var color = "White"; function ChangeColor () {   var  anothercolor = "Red";   function Swapcolors () {   var tempcolor = Anothercolor;   Anothercolor = color;   color = Tempcolor;   Here you can access the color Anothercolor tempcolor}swaocolors ();//Here you can access the color Anothercolor}changecolor ();//access only to color

The above code has a total of three execution environments, namely the Global Environment ChangeColor () Local environment swapcolors () Local environment, the global environment has a variable color and in ChangeColor () has anothercolor this variable and SWAPC Olors () This function, there is a tempcolor in Swapcolors (), as to why some places do not have access to it?
A scope chain is where the internal environment can access all external environments, but in turn it does not, and each environment can search up the scope chain to query for variables and functions, but they cannot search down
Remember that there is no block-level scope in JS
JavaScript is not the same as C JAVA C # He doesn't have his own block-level scope, and of course you can simulate it in some way, as we'll talk about later.
for (var i =0; i<10;i++) {      console.log (i);} alert (i);  i=10

If it is Java then the variable I will be destroyed immediately, but in JS! Still exists
query identifiers in JS
var color = "Blue"; function GetColor () {     //var volor = "Red";      return color;} Alert (GetColor ())  //blue//If you remove the comment from the GetColor () then the red is returned

Yes, in JS, the identifiers will be searched on the web through the scope, until they are found.
GC garbage collection in the browser in fact, all you need to know about a concept here is to dereference it.
Once the data is no longer useful, it is best to set it to NULL to release its reference
to such var a = "csdn";//useless. A = null//dereference

Summarize
    • The exact process of copying from one variable to another creates a copy of the value
    • The value of a reference type is an object, saved in heap memory
    • A variable that references a type value is actually a pointer
    • Replication of reference types essentially duplicates a pointer they point to the same object at the same time
    • Learn a new tool instanceof
    • Learned the scope chain
    • Understand the 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.