in Javascript

Professional Javascript For Web Developers – 复习笔记(Variables, Scope, and Memory)

variables -primitive & reference

ECMAScript variables may contain two different types of data: primitive values and reference values
Primitive values are simple atomic pieces of data, while reference values are objects that may be made up of multiple values.
The five primitive types were discussed in the previous chapter: Undefined, Null, Boolean, Number, and String.
variables are said to be accessed by value
Reference values are objects stored in memory
When you manipulate an object, you’re really working on a reference to that object rather than the actual object itself. For this reason, such values are said to be accessed by reference.reference value is assigned from one variable to another, the value stored on the variable object is also copied into the location for the new variable.
When a primitive value is assigned from one variable to another, the value stored on the variable object is created and copied into the location for the new variable.

The difference is that this value is actually a pointer to an object stored on the heap. Once the operation is complete, two variables point to exactly the same object, so changes to one are reflected on the other

var obj1 = new Object(); 
var obj2 = obj1; = “Nicholas”; 
alert(; //”Nicholas”
Argument Passing

All function arguments in ECMAScript are passed by value. This means that the value outside of the function is copied into an argument on the inside of the function the same way a value is copied from one variable to another. If the value is primitive, then it acts just like a primitive variable copy, and if the value is a reference, it acts just like a reference variable copy.

When an argument is passed by value, the value is copied into a local variable
When an argument is passed by reference, the location of the value in memory is stored into a local variable, which means that changes to the local variable are reflected outside of the function.

var count = 20; 
var result = addTen(count); 
alert(count); //20 - no change 
alert(result); //30
function setName(obj) { = “Nicholas”; 
var person = new Object(); 
alert(; //”Nicholas” (changed as reference)

延伸阅读 :What is the Execution Context & Stack in JavaScript?
All variables, primitive and reference, exist within an execution context (also called a scope) that determines the lifetime of the variable and which parts of the code can access it.
Execution context can be summarised as follows:
➤ Execution contexts exist globally (called the global context) and within functions.
➤ Each time a new execution context is entered, it creates a scope chain to search for variables
and functions.
➤ Contexts that are local to a function have access not only to variables in that scope but also to variables in any containing contexts and the global context.
➤ The global context has access only to variables and functions in the global context and cannot directly access any data inside local contexts.
➤ The execution context of variables helps to determine when memory will be freed.

There are only two primary types of execution contexts, global and function (the third exists inside of a call to eval())

No Block-Level Scopes
for (var i=0; i < 10; i++){ 
alert(i); //10
In languages with block-level scoping, the initialization part of the for statement defines variables that exist only within the context of the loop. In JavaScript,the i variable is created by the for statement and continues to exist outside the loop after the statement executes <h6>variable declare When a variable is declared using var, it is automatically added to the most immediate context available. In a function, the most immediate one is the function’s local context; in a with statement, the most immediate is the function context. If a variable is initialized without first being declared, it gets added to the global context automatically
function add(num1, num2) { 
var sum = num1 + num2; return sum; 
var result = add(10, 20); //30 
alert(sum); //causes an error since sum is not a valid variable
function add(num1, num2) {
 sum = num1 + num2; return sum; 
var result = add(10, 20); //30 
alert(sum); //30

JavaScript is a garbage-collected language, meaning that the execution environment is responsible for managing the memory required during code execution
The basic idea is simple: figure out which variables aren’t going to be used and free the memory associated with them. This process is periodic, with the garbage collector running at specified intervals


The most popular form of garbage collection for JavaScript is called mark-and-sweep. When a variable comes into context, such as when a variable is declared inside a function, it is flagged as being in context. Variables that are in context, logically, should never have their memory freed, because they may be used as long as execution continues in that context. When a variable goes out of context, it is also flagged as being out of context.

Reference Counting

A second, less-popular type of garbage collection is reference counting. The idea is that every value keeps track of how many references are made to it.

Keeping the amount of used memory to a minimum leads to better page performance. The best way to optimize memory usage is to ensure that you’re keeping around only data that is necessary for the execution of your code. When data is no longer necessary, it’s best to set the value to null, freeing up the reference — this is called dereferencing the value.Keep in mind that dereferencing a value doesn’t automatically reclaim the memory associated with it. The point of dereferencing is to make sure the value is out of context and will be reclaimed the next time garbage collection occurs.