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 think back to what you said the previous time the assumptions have been forgotten points here
Today, let's talk about variables and scope issues.
Main contents of this chapter
    • Basic types and reference types
    • Operating Environment
    • Garbage collection (understanding is possible)
The basic type and reference type JS may include 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? See below
It's OK to create a variable and assign it a value. For reference types we are able to add and remove its properties and methods
var csdn = new Object (); csdn.blog = "Tomihaohao"; alert (Csdn.blog)   //Tomihaohaovar name = "Csdn"; name.age = 123;alert ( Name.age)  //undefined//the same piece of code why is the result different?

in JS, you can only add dynamic properties to the value of a reference type.
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 the number of references:     Do you remember the function of the last chapter? Let's recall that the function parameters in JS are very flexible, remember arguments[]? Let's say you forgot to click here.
Today, I'm going to talk about the number of references in JS.
Please remember The parameters of a function in JS are 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 another.
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 has run out.

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

The most important concept of the runtime environment and scope in javascript: the operating Environment!
    • The operating environment defines the other data that variables and functions have access to and determines their respective behavior
    • Each run environment has a variable object associated with it
    • In the browser, the Window object is the global runtime environment, and they only have the ability to destroy when the app exits.
    • Each function has its own operating environment.
    • When code runs 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 visit color Anothercolor tempcolor}swaocolors ();//Here you can access color anothercolor}changecolor ();

The above code together has three operating environments, each of which is the Global Environment ChangeColor () Local environment swapcolors () Local environment, there is a variable color in the global environment and in ChangeColor () there is anothercolor this variable and SWA Pcolors () This function, there is a tempcolor in the Swapcolors (), as to why there is no access to the local visitors?
The scope chain is that the internal environment can access all the external environment through it, but in turn does not work, each environment can search up the scope chain, to query variables and functions, but they can not 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

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

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, everyone here just needs to know a concept, that is, to dereference.
Once the data is no longer practical, it is best to set it to NULL to release its reference
to such var a = "csdn";//useless. A = null//dereference

Summarize
    • The detailed process of copying from one variable to another will create a copy of this 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 replicates a pointer they point to the same object at the same time
    • Learn a new tool instanceof
    • Learned the scope chain
    • Understand the GC



JavaScript Small white Study Guide 1---0

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.