JavaScript variables, scopes
The data types of JavaScript are divided into basic data types and reference data types,
1. The basic data types include NULL, Undefined, Number, Boolean, and string types, and these five basic data types can be determined by the TypeOf method,
Alert (typeof a); The result is ' null ', ' undefined ', ' Number ', ' Boolean ', ' string ', assignment of the basic type is worth passing, example: when you pass the value of a variable A to another variable B,
The two variables a, b have their own values, the operation of a does not affect the value of B, and vice versa,
2. Reference data types,
A. Arrays, objects, and so on are reference data types, reference data types can add properties, but basic data types are not (although adding attributes to the base data type does not give an error, but does not take the value of the property),
var person = new Object;
Person.name = ' John ';
alert (person.name); The result is ' John ';
B. The decision of the reference data type cannot use the TypeOf method, the Instanceof method should be borrowed,
var arr = new Array ();
Alert (arr instanceof Array); Determines whether Arr is an instance of Arry, returns True, does not return false, returns true here,
Any reference data type that uses obj instanceof object returns True because object is the base class of the Suoyou class,
C. The reference data type assignment is a reference pass, not a worthwhile pass, example: When a variable A of a reference type is assigned to B, only the reference is copied to B, and A and B point to the same object in memory,
A change will inevitably lead to a change in B, vice versa,
var a = new Object ();
A.name = ' John ';
var B = A;
B.name = ' Smith ';
alert (a.name); Since the references to variables A and b all point to the same in-memory object, so B changes the time, the value of a also changes, so this pops up ' Smith ';
3. The transfer of parameters in the function,
In the "JavaScript Advanced Programming" book, the parameters of a function are passed, and all values are passed (both the base data type and the reference data type are worth passing when passed as parameters),
Give an example of this,
function SetName (obj) {
Obj.name = ' Smith ';
obj = new Object ();
Obj.name = ' Xiaoyang ';
}
var person = new Object ();
Person.name = ' John ';
SetName (person);
alert (person.name); The result is Smith; "JavaScript advanced Programming" explains this, if the person is passed by reference, then in obj.name = ' Xiaoyang ';
At the time of assignment, the value of the person.name should be changed to ' Xiaoyang ', and the result is ' Smith ', < and my personal understanding is that the reference to the parameter obj was altered in SetName as a obj=new Object ().
And since the reference has changed, the change in the value of obj will not affect the value of the external person variable, >
var person = new Object ();
Person.name = ' John ';
var obj = person; References to obj and person here point to the same object,
obj = new Object ();
Ob.name = ' Smith ';
alert (person.name); The result is ' John '; If you refer to "JavaScript Advanced Programming" (parameter passed as value pass, non-argument pass as reference pass), the value here should be ' Smith ' and the actual value is ' John ',
Because Obj=new object () changes the reference before obj, and the person has pointed to a different object, the change of the obj value does not affect the value of the person,
Second, the scope of variables,
The 1.JavaScript variable scope and Java are a little different, in JavaScript, each function has an execution environment, the global execution environment is the outermost one of the execution environment,
and JavaScript code is executed in the top-down order, so the following code if not defined in the specified function, then its execution environment is the global execution environment, all the variables and functions are defined on the window,
if (true) {var num = = 11;} alert (num); The result is 11, with alert (window.num);
2. Inside the function, the scope of the variable cannot exceed the function, but the intrinsic function can access the variables of its previous layer function,
var num = = 9;
function Add () {
var a = num + 1; /Here you can access the outer num,
var B = 1 + C; The exception is thrown here, because in the execution environment add, there is no variable C
Function Plus () {
var C = A + b;/Here you can access the outer layers A and B,
}
}
JavaScript uses variables, first in the execution environment, if the query is not up to the next level of the query, until the outermost execution environment, but not in the internal execution environment to query,
3. Extend variable scope, with and catch statements
function Add () {
var qs = '? Debug=true ';
With (location) {
var url = href + qs;
}
return URL; The URL here crosses the WITH statement, can be accessed outside of the WITH statement,
}
In JavaScript advanced programming, errors caught by catch statements are added to variable objects in the execution environment, not in variable objects in the Catch statement, so the error object can be accessed outside the Catch statement,
The following code, found in Chrome, cannot access the Err object outside of the catch
function Add () {
try {
var a = b + 1;
return A;
} catch (Err) {
Console.log (Err.message);
}
Console.log (' [' + Err.message + '] '); The Err object cannot be accessed here,
};
Add ();
JavaScript Learning note "Two"