Global scope: Window Global

Variables and constants can store any data type

Js data type

Base data type (value type)

  • number
  • string
  • boolean
  • null
  • undefined
  • symbol

Reference data type

object

  • {} Ordinary objects(json)
  • [] An array of
  • $/ / ^ regular
  • The Math objectThe data type

function

  • functionCommon function
  • classclass

Operating rules

Value type (basic data type) :

var a=12;

  • 1) at firstThe current scope declares a variableA, there is no assignmentundefined
  • 2) when theThe former scopeIn theOpen up a location store12
  • 3) Let declared variables and stored in 12To associate

Direct by value operation: copy the original value in a new space, and the original value has no relation. The variables do not affect each other

Only one value can be stored in a variable

Object data type :(operating by memory space)

  • 1, firstCreate a variableDeclaring a function name is the same as declaring a variable.
  • 2. The browser is itCreate a new memory spaceIn order to facilitate other places to find this space will be given spaceAssign a hexadecimal address(Hexadecimal: 0-9 a-f)
  • 3,Store key-value pairs of objects into memory in a certain order
  • 4. Assign an address to a variable (or something else, such as an event) that the variable can use to find memory and operate on it

Operation is the reference address of the space: assign the original space address to the new variable, but the space has not been cloned, or a space, so that there will be multiple variables associated with the same space, there will be influence between each other

var a={name:'ha ha'}
1, declare variable a2, open up space3111fff000 space to store key-value pair name:'ha ha'
4, assign 111fff000 to ACopy the code

Function operations:

Create a function:

  • 1. Open one firstNew memory space(for itsA hexadecimal address is assigned)
  • 2, write the function bodyJs code as "string"Store it in space (soIt makes no sense to create a function without executing it)
  • 3. AssignAddress assigned toThe statementThe function namefunction fn(){}andvar fn=function(){}Both declarations operate in the same way. They both declare a name in the current scope.

Execute function – Purpose: Execute code in the function body

  • 1. When the function is executed, the browser willForm a new private scope(can only execute code in the function body), which is executed by code in the function body. Each time the function is executed, a new private scope is formed
  • 2. Before executing the codeCopy the string that created the function into a real JS expressionExecute in private scopes from top to bottom.

A function can be executed N times without interfering with each other, because each execution of the function performs the above steps to recreate the private scope.

The sample

var a=10;
var b=a;
b=14;
console.log(a)  => 10

// Change the key value in the same space address
var c={name:'haha'}
var d=c;
d.name='quququ';
console.log(c) =>  {name: "quququ"}

// Redefining the address does not change the original space
var m={name:'aaa'}
var n=m;
n={name:'bbb'}
console.log(m.name) => aaa
Copy the code

“Closures” :

Executive function formed when the private scope of private variables of the function body wrapped (protected), in a private scope operating private variables have nothing to do with the outside world, the outside world is unable to direct the operation of the private variables, we put the function of this mechanism is called closure (function, form a private scope, to protect the private variables inside from outside interference, This protection mechanism is called closure.

However, many people now believe that a closure is a mode in which functions are executed, forming an undestroyed private scope that can store something in addition to protecting private variables

Stack memory in JS:

Stack memory: commonly known as scope (global/private scope)

Provides an environment for js code to execute (js code is executed in stack memory)

Basic datatype values are stored directly in stack memory (because basic datatype values are simpler, they create a place in stack memory to store values)

When stack memory is destroyed, the values of the basic data types that were stored are also destroyed

Heap memory:

Storing values of reference data types (equivalent to a repository of storage)

Objects store key-value pairs

Functions store code strings

Memory processing:

Heap memory handling:

Var o={} The heap memory of the current object is occupied by the variable o.

Destroy method o=null; Null object pointer (which does not point to any heap memory), then the original heap memory is not used.

Google Chrome automatically releases unused heap memory during idle time

Stack memory processing:

In general, the stack memory is formed when the function is executed. After the function is executed, the browser will release the stack memory automatically.

In special cases, stack memory cannot be freed after the function completes execution (the return reference data type is referenced elsewhere).

The global scope is executed when the page is loaded and destroyed when the page is closed.