The ECMAScript variable may contain values for two different data types: the base type value and the reference type value. A primitive type value refers to a simple data segment, whereas a reference type value refers to an object that may consist of multiple values. 5 Basic data types: Undefined, Null, Boolean, number, and String.
these 5 basic data types are accessed by value because you can manipulate the actual values that are saved in the variable. The value of a reference type is an object that is saved in memory. Unlike other languages,
JavaScript does not allow direct access to the in-memory location, which means that the object's memory space cannot be manipulated directly. When you manipulate an object, you are actually manipulating the object's reference rather than the actual object. To do this, the value of the reference type is accessed by reference.
Dynamic PropertiesFor values of reference types, we can add properties and methods to them, and you can change and delete their properties and methods. Objects are not destroyed, the properties and methods that are set are always present. You cannot set properties and methods on a base type's value.Copy variable values If you copy a base type value from one variable to another, a new value is created on the variable object and the value is copiedto the location assigned to the new variable. These two variables can participate in anydo not affect each other. (Copy the value of the variable) when a value of a reference type is copied from one variable to another, the value stored in the variable object is also copied to theThe space allocated for the new variable. The difference is that a copy of this value is actually a pointer, and this pointer points to a memory stored in the heapan object. When the copy operation is finished, two variables will actually refer to the same object. Therefore, changing one of the variables will affect the othera variable. (The reference pointer is copied) Passing Parameters The parameters of all functions in ECMAScript are passed by value. in other words, the values outside the function are copied to the parameters inside the function.number, just as you would copy a value from one variable to another. access variables are both by value and by reference, and
parameters can only be passed by value .
When you pass a value of a primitive type to a parameter, the passed value is copied to a local variable (that is, a named parameter, or , in the ECMAScript concept, an element in the arguments object).
| 12345678< /td> |
function addten (num) { &NBSP;&NBSP;&NBSP;&NBSP; num +=10; &NBSP;&NBSP;&NBSP;&NBSP; return num; var count =; var result = Addten (count); alert (count); //20 alert (result); //30 |
to the function is a reference to the value, Changes to its properties are externally visible in the function, but are not visible externally when overridden with a new reference.
| 123456 |
functionsetName(obj){ obj.name = "staven";}var person = newObject();setName(person);alert(person.name); //staven |
Normal assignment
| 1234 |
vara = 1;varb = a; //赋的是a的复制值b ++;alert(a); //"1" b的修改不影响a |
assigning values to Objects
| 1234 |
vara = [1];varb = a; //赋的是a的引用 b[0] ++;alert(a); //"2" b的修改对a也有效 |
Parameter pass-through: a copy of the value passed to the function, the modification of the function is not visible externally
| 123456789101112 |
var a = 1;var b = 2;function change(a,b) { var c = a; a = b; //用新引用覆盖 b = c; alert(a); //"2" alert(b); //"1"}change(a,b);alert(a); //"1" alert(b); //"2" |
Pass-through: a reference to a value that is passed to a function that is externally visible in the function's modification of its properties, but which is not visible externally when overridden with a new reference
| 12345678910111213 |
var a = [1, 2, 3];var b = [5, 6];function change(a,b) { a[0] = 4; //对其属性的修改外部可见 var c = a; a = b; //用新引用覆盖 b = c; alert(a); //"5,6" alert(b); //"4,2,3"}change(a,b);alert(a); //"4,2,3"alert(b); //"5,6" |
A, B is a variable in the change function that passes a A/b reference to the function when it is called, but does not alter A/b in the global. because overwriting with a new reference is not visible externally, the function simply gets the reference and does not have the power to change the reference.
| 1234567891011 |
vara = [1, 2, 3];var b = [5, 6];function change() { varc = a; a[0] = 4; a = b; b = c;};change();alert(a); //"5,6"alert(b); //"4,2,3" |
also have to mention the block-level scope of JS, this put some language must be reported undefined error, because JS has no block-level scope, so it can not find the variable in the change, a B will be consciously to the upper level to find, so here is a, a, a, a reference to the global variable. The comparison of the values compared to the value of the comparison
is the reference.
parameter passing angle understanding closure
| 12345678 |
var add_handlers = function (nodes) { var i; for (i = 0, l = nodes.length; i < l; i ++) { nodes[i].onclick = function (e) { alert(i); // 当然这里的结果必然是每次alert的都是节点总数。。。。 } }}; |
at this point I is a reference to the variable of the parent function scope. a reference to I is passed to alert when I set the OnClick event to each node, and when I click on the node to trigger the OnClick event, the value of I has become the total number of nodes.
| 1234567891011 |
var add_handlers = function (nodes) { var i; for (i = 0, l = nodes.length; i < l; i ++) { nodes[i].onclick = function (i) { return function(){ alert(i); } }(i); }}; |
This modification is correct because the copy of the value of I is passed in at this time.
Detection Type The typeof operator is the best tool for determining whether a variable is a string, a numeric value, a Boolean, or a undefined. If you changeThe value of the amount is an object or null, the TypeOf operator returns "Object". If the variable is an instance of a given reference type, theninstanceof operator returns TRUE.
| 12345678 |
function setname (obj) { &NBSP;&NBSP;&NBSP;&NBSP; obj.name = "Staven" &NBSP;&NBSP;&NBSP;&NBSP; obj = new object (); &NBSP;&NBSP;&NBSP;&NBSP; obj.name = "Bob" } var person = new object (); setname (person); alert (person.name); //staven |
From for notes (Wiz)
JavaScript parameter passing and variable copying