JavaScript-- gallop script execution's Web page (3)

Fifth, objects and functions

ECMAScript is actually a set of data objects ( properties ) and functions ( methods ) collection, is a complex reference data types, reference data types and functions of the array can be seen as a special kind of object.

 

1. Creating an object

You can create an object using the constructor ( Object is the root object of all objects, directly or indirectly, a normal object will inherit Object ), such as:

var person = new Object();

person.name = "briup";

person.age = 22;  

May also be used to create object literal represents an object method, using different attributes ',' split, with between attribute names and values of ':' segmentation attribute name may not double quotes, when the attribute name contains certain special characters use single / double quotes, the attribute value is generally constant or specific values may be variable, such as:

each person = {

    name : "briup",

    age : 22

};

 

2. Access to delete an object property

Using dot notation access attributes: the right of property must be based on a simple identifier named after, as person.name;

Use bracket notation to access properties: in brackets must be an expression that evaluates to a string, you can access variable attributes, a syntax error if the property name contains a character or attribute name using keywords or reserved words It may be used in bracket notation access, such as person [ "name"].

 

Use delete to delete attributes: such as delete obj1.name. delete just disconnect the connection properties and host objects without operational attribute area in the property, and delete only deletes its own property, inherit property can not be deleted. When the object is destroyed, in order to prevent memory leaks, traverse the object attributes in order to remove all attributes.

 

3. Detection of object properties

in can detect whether a property is owned attribute of an object or inherit property;

hasOwnProperty () can detect whether the attribute is given its own object properties, inheritance property returns to false ;

propertyIsEnumerable () can detect whether the attribute is given its own properties of the object, and the property is enumerable. Usually it consists of JS property to create code is enumerable, but also can use a special method to change the enumerable properties. For example :

There he = {

x:1

}

console.log (x in o); // true x is o property

o.hasOwnProperty ( "x"); // true x is o own property

o.hasOwnProperty ( "y"); // false o attribute does not exist in the y

o.hasOwnProperty ( "toString"); // false toString does not belong to own property

o.propertyIsEnumerable ( "x"); // true own property enumerable

o.propertyIsEnumerable ( "toString"); // false inherited property can not enumerate

 

Because all objects inherit from Object , so the Object of any type have properties and methods that also exist in other objects. Object commonly used methods:

constructor; // save the user to create the current object function

hasOwnProperty (propertyName); // check whether a given attribute name attribute is its own object,

toString (); // Returns a string representation of the object

valueOf (); // represents returned object string, numeric, boolean value.

propertyIsEnumerable (propertyName); // check a given property exists in the current object instance

isPrototypeOf (object); // check whether the object is passed prototype

toLocaleString (); // Returns a string representation of the object, the region corresponding to the execution environment of the string

 

4. The traverse object

Using enhanced for loop through objects and their attributes, such as:

var obj = {

name:"terry",

age:12

}

for(var key in obj){

// get the property value

var value = obj[key];

console.log(key, value);

}

obj can be an array or object; Key represents an attribute name of the object or the index of the array; each time from when running obj acquire a property name or an index assigned to the Key (if the array is stored in the object, the normal printing index, the object is called toString () attribute names and values of the print object), and then execute the loop body .

 

5. Object Serialization

Object serialization refers to converting the object into a string state, deserialization may be, to restore the function of the object string. RegExp, Error objects, undefined value can not be serialized and deserialized.

JSON.stringify (obj); // serialize objects into Json string , only the sequence of its own object properties enumerable

JSON.parse (jsonStr); // deserialization

 

6. Description of function

Function allows us to package a series of code to accomplish a specific task. When you want to accomplish a task, simply call the appropriate code. Browser provides us with a lot of built-in methods, we do not need to write code, just call the method to complete specific functions, such as sorting, reverse, etc. array manipulation functions, random number generation and other functions.

 

7. function declaration

Function from the function keyword statement, followed by the function name, the function name as a parameter list, the list braces content as a function of the body. Also an anonymous function (no function name of the function) function can be assigned to a variable, in this way becomes a function expression.

Function declaration:

function function name ( parameter list ) {

// function body

}

Function expression:

var function name = function function name ( parameter list ) {

// function body

}

Parser when loading data into the execution environment, will be the first reading function declarations, and make it available before executing any code; but for the function expression is, when the actuator to perform a function expression code will really the interpreted.

 

8. The internal property function

Only attribute to determine their value when the function is running, it can only be accessed within the function.

Internal attributes There are three main functions:

Parameter : receiving shortcut argument

arguments: an array of class objects in order to receive all arguments , arguments: {0: argument 1, 1: arguments 2,2: argument 3}

this: current object ( execution environment object)

this value of the relevant The function is called :

If the function using "()" to call that look "()" in front of is not the function name, and if so, look at the front of the function name there is no object, if there is, the this point to the object, or the global object ( window /, Ltd. Free Join ) ;

If by call, apply invoked, the this is a call to action occurred objects .

 

Function has a length property (non-internal property), for determining the number of formal parameters.

 

9. calling function

After a good function declaration does not directly allow need to be invoked in order to run. Call the method are:

Function name ( argument list );

Function name .call ( execution environment object this, the argument list );

Function name .apply ( execution environment object this, the argument list array );

 

10. scoping issues

If a variable declared in a function, then this variable can only be accessed in a function when the function is finished, this variable will be released.

In addition to the features in the function, JavaScript is var absence of local scope, be careful not to repeat the statement, ECMAScript new version recommended for let to declare variables. When the code runs nodejs time in, A not visit; but when it runs in a browser, A value of 3 .

function foo(){

if(true){

// local scope, A only applies to the current block

There are a = 3;

}

console.log(a);

}

 

11. A value transfer and passed by reference

The basic data types of variables can be stored directly in the operation of an actual value of a variable, the parameters passed when the actual value is passed, i.e., when the stack area and copying the original copy of the same value as the variable to a new variable, a new variable value.

Reference data type variables can not directly manipulate the memory space objects, only the operation object. Can add properties and methods reference type variable, you can also delete and change its properties and methods. When the transmission parameters are transmitted reference address, i.e. replication is to copy the original variables stored address corresponding to a memory heap to a new variable, a new value of the variable.

 

 

Guess you like

Origin www.cnblogs.com/wodeqiyuan/p/11408569.html