Prototype type, reference type

Primitive types

Primitive data types are some simple data, such as strings, numbers, etc., such as: true and 25, these data are stored directly in the memory variables. ECMAScript 5 provides us with five original type:

Types of

data

Explanation

Boolean

true、false

Boolean value, true or false

Number

12,12.5, in

Integer, floating point, the special value NaN (Not a number)

String

'hello'、"hello"

Double or single quotes in quotation marks or a string of characters

Null

null

Only a null value

Undefined

undefined

Only a value of undefined. Any statement contained only unassigned variables will be assigned to undefined.

When the storage variable is the type of the raw data directly to a memory storing data variables. When we store raw data type variable to another variable, in fact, is the value of the variable to store a copy saved to another variable.

Because each variable is using its own independent storage space to save the original type of data, so when we change a variable's data will not affect the other variables in the data.

Detecting the type of the raw data

The best way to detect the type of the raw data using typeof operator, the operator returns a string representation of the data type.

Method primitive data types

Although the strings, numbers, Boolean data type is the original, but there are many methods can be used (null and undefined no method).

Reference types

Reference type of data a little more complicated, it refers JS objects, similar to a class in other programming languages. Object is an unordered list by a series of key-value pairs (attribute names and values) composition. ECMAScript provides us with several built-in objects as follows:

      Object     Array     Date     RegExp(regular expression)      Function      Error      String       Number         Boolean

Examples we can create an object using the new operator and constructors, can also create an instance by literal manner. For example, the following code by way of example and creating a new operator Object object obj and saves the instance variable, the type of reference data is not directly stored in the memory variables, variables stored in memory is only reference type data address (pointer) in memory, a reference to the time when we will type variable to another variable, in fact, save a copy of the address of a variable to another variable, then the two variables point to the same object. After changing the attribute value of an object name through a variable which will affect the other variable.

While JavaScript is a language with a garbage collection mechanism, we do not care about the type of reference data memory allocation problem. But when we are no longer a reference type variable is used, it is best to lift a reference to the instance variables, it is a good recycling garbage collection mechanism in a timely manner so as to free up memory. Dereference easiest way is, the variable assignment to null.

By way constructor

We can use the new operator for each instance of a built-reference type, for example:

var items = new new the Array (); 

    var now = new new a Date (); 

    var FUNC = new new Function ( 'A', 'B', 'return A + B' ); 

    var obj = new new Object (); 

    var Re = new new the RegExp ( '\\ + D' ); 
    
    var error = new new error ( 'your browser does not support the canvas');

By literal way

Enables us to use literally without the use of the new operator and the constructor may be instantiated reference type.

Object literals and Array of

Created by Object literal way of example, we can also use the string as the attribute name, when we attribute names with spaces or other special characters, even though the wording of these two differences in grammar, but their role is identical.

Literal function

When we create a method using the most literal way way. Use Function constructor way rare, type create an instance of Function by literal way

function the reflect (value) {
             return value; 
        } 
        // the code above is equivalent to: 
        var the reflect = new new Function ( 'value', 'return value');

The literal than using the constructor understood and easier to write. Use the constructor is not good for debugging code, JavaScript debugger does not recognize them correctly.

Regular expression literals

JavaScript regular expressions to provide a literal form of:

var the pattern1 = / \ D + / GI;
         // equivalent to: 
        var pattern2 = new new the RegExp ( '+ D \\', 'GI');

Regular literal form of expression than the structural form function is easier to handle, we do not care whether the string of characters need to be escaped. When using the constructor, the matching pattern is a string transfer, it is necessary to backslash escaped.

When the built-reference type is instantiated, using literals or constructor can, no right or wrong, but in the instance of type Function recommended literal form.

Property access objects

Properties are stored as key-value pairs in the object, access the properties of the most common way is to use points, but can also be accessed in the form of square brackets:

var obj = {
            name: 'zhansan',
            age: 34
        };

        console.log(obj.name);      //'zhansan'
        //等价于:
        console.log(obj['name']);  //'zhansan'

Detecting reference data type

var ARR = new new the Array ( '. 1', 2, to false );
     var DATE = new new a Date ();
     var RP = new new the RegExp ( '\ D +', 'G' );
     var FUNC = new new Function ( 'A', ' B ',' a + B return ' );
     var obj = new new Object ();
     var error = new new error ( "your code error' ); 

    // use typeof operator detection reference type 
    the console.log ( typeof ARR) ;         // 'Object' 
    the console.log ( typeof DATE);       // 'object'
    console.log(typeof rp);         // 'object'
    console.log(typeof func);       // 'function'
    console.log(typeof obj);        // 'object'
    console.log(typeof error);      // 'object'

When using a reference type detection typeof, except Function, other types of references is not to be correctly identified. This time we can use instanceof to detect them.

instanceof operator need an object constructor and an object instance as a parameter, then if the instance is created using the constructor returns true, otherwise returns false:

 

var ARR = new new the Array ( '. 1', 2, to false );
     var DATE = new new a Date ();
     var RP = new new the RegExp ( '\ D +', 'G' );
     var obj = new new Object ();
     var error = new new error ( "your code error ' ); 

    // use instanceof operator detection reference type 
    the console.log (ARR instanceof the Array);          // to true 
    the console.log (DATE instanceof a Date);          // to true 
    the console.log (RP instanceof RegExp);         // true
    console.log(obj instanceof Object);        // true
    console.log(error instanceof Error);       // true

Because all of the reference types are inherited from Object, so every reference to an instance instance of the type is actually the Object:

console.log(arr instanceof Object);         // true
    console.log(date instanceof Object);        // true

Original wrapper types

JavaScript doubt the most is probably the original packaging type. JavaScript provides us with three kinds of packaging types (String, Number, Boolean).

Original packaging type is a reference type, when the string, number or Boolean values ​​are read, the original wrapper type is automatically created in the background. E.g:

var name = 'zhangsan';
    var firstChar = name.charAt(0);
    console.log(firstChar);

Guess you like

Origin www.cnblogs.com/zhengedeboke/p/12076286.html