JavaScript web tutorial test the variables

  JavaScript variables and other variables is very different languages. JavaScript variables are loose type (not mandatory) essentially determines that it is only used to save a name of a particular value at a particular time only. Since there is no definition of what type of data a variable rule value must be preserved, and the value of the data type of a variable can change during the lifetime of the script.

  Variables and Scope

  1. The reference value of the basic type and the type of

  ECMAScript variable may comprise two values ​​of different data types: basic type value and the reference value type. Primitive values ​​are those stored in the stack segment simple data memory, i.e., the value of such a position is completely stored in memory. And reference type values ​​are those objects stored in the heap memory means is stored in a variable is really just a pointer that points to another location in memory, the position of the object is stored.

  When you assign a value to a variable, the parser must determine this value is a basic type value, or a reference type value. There are several basic types of values: Undefined, Null, Boolean, Number, and String. These types of memory, respectively, in the space occupied by fixed size, their value is stored in the stack space, we accessed by pressing the value.

  PS: In some languages, a string is represented as an object, it is considered the reference type. ECMAScript give up this tradition.

  If the assigned value is a reference type, you must assign a value to this space in the heap memory. Since the size of this value is not fixed, it can not save them into the stack memory. But fixed, it is possible to save the memory address to memory addresses in the stack memory size. Thus, when a query reference type variable, memory address to start reading the stack, and then find the value of the heap through the address. For this, we call it by reference access.

  2. Dynamic Properties

  Define basic type value and reference types by value are similar: to create a variable and assign this variable. However, when the value stored in the variable later, operations may be performed on different types of values ​​are quite different.

  var box = new Object();

  box.name = 'zhang';

  alert(box.name);

  If the value of the basic types of add properties, the problem will appear.

  each box = 'Zhang';

  box.age = 17;

  alert(box.age);

  3. Copy the value of the variable

  In the replication variables, basic types and reference types are different. Basic types of replication is a value in itself, and reference types are copied address.

  each box = 'Zhang';

  was box2 = box;

  Although a copy is box2 box1 and illustrated but can be seen that it is completely independent. In other words, independently of each other when the two variables are operating.

  var box = new Object();

  box.name = 'zhang';

  was box2 = box;

  In reference types, box2 is actually a box, because they point to the same object. If the name attribute of the object is modified, and the value box2.name box.name output will be modified accordingly lost.

  4. The transmission parameters

  ECMAScript functions All parameters are passed by value, implying that is, the parameter will not be passed by reference, although the variable basic points and reference types.

  function box(num) {

  a + = 10;

  return num;

  }

  var n = 50;

  was resulted = box (num);

  alert(result);

  alert(num);

  The above code, the value of the parameter passed is of a primitive type. The function where num is a local variable, and the outside num does not have any contact.

  The following example is given as a parameter reference type.

  function box(obj) {

  obj.name = 'zhang';

  }

  var p = new Object();

  box(p);

  alert(p.name);

  function box(obj) {

  obj.name = 'zhang';

  var obj = new Object();

  obj.name = 'Mr.';

  }

  Concluded, ECMAScript function parameters will be local variables, that is, not passed by reference.

  5. Detection Type

  To detect the type of a variable, we can be determined by typeof operator. Such as:

  each box = 'Zhang';

  alert(typeof box);

  Although typeof operator check the basic data types in time is very easy to use, but when the reference type is detected, it is not so easy to use. In general, we do not want to know that it is not an object, but in the end it would like to know is what type of object. Because arrays also object, null is Object, and so on.

  At this time we should use the instanceof operator to view.

  each box = [1,2,3];

  alert(box instanceof Array);

  was box2 = {};

  alert(box2 instanceof Object);

  was box3 = / g /;

  alert(box3 instanceof RegExp);

  was box4 = new String ( "Zhang");

  alert(box4 instanceof String);

Guess you like

Origin www.cnblogs.com/coffees/p/12331238.html