JavaScript Note 3: Reference Type

Source: Internet
Author: User

JavaScript Note 3: Reference Type

1. Object

Create an Object,

Var person = new Object (); var person = {

Person. name = "guoliang"; name: "guoliang ",

Person. age = 20; age: 20

};

In addition, attribute names can also use strings, and the second type belongs to the object literal representation.

You can use "." or "[]" to access object attributes. You can only use "[]" to access attribute names that contain non-letters and non-numbers. "." Is generally recommended.

2. Array type

The array in JavaScript is different from other languages. It can store any type of data, and its size is dynamically changeable, and will automatically increase as the data increases.

Var color = new Array (); // create a color Array

Or var color = [];

Var color = new Array (20); // create an Array with a length of 20

Var color = new Array ("red", "blue", "green ");

Or var color = ["red", "blue", "green"];

New can be left blank and the results are the same

You can use length to get the length of the array, but not only can read-only. length can also remove array items or add new items at the end. If the value is greater than the current size of the array, the array recalculates the length value, that is, the length is equal to the index value of the last item plus 1.

Array. isArray (); you can determine whether the value is an Array.

The push and pop operations can be performed at the end of the array, while the push and shift operations can be performed at the end of the array. Unshift (front-end) and pop (end) enter and exit.

Sorting: reverse () and sort (); they compare strings, but sort () may be 5> 10 during comparison, therefore, use sort (compare (comparison function ));

Function compare (value1, value2 ){

Return value2-value1; // Ascending Order

}

Concat (); // concatenate an array

Slice (); // get the value in the array

Splice (); // delete/insert/replace

Index (); // before and after

LastIndex (); // forward from the back

Every (); filter (); forEach (); map (); some (); // Iteration Method

Reduce (); reduceRight (); // Method for downgrading

3. Data Type

The date that is saved in milliseconds since midnight, January 1, January 1, 1970, UTC.

Data. parse (); Data. UTC (); Data. now ();

ToDataString (); toTimeString (); toLocalDataString (); toLocalTimeString (); toUTCString ();

4. RegExp type

ECMAScript supports regular expressions through the RegExp type.

Var expression =/pattern/flags;

Pattern allows any simple or complex regular expressions. flags are used to indicate the behavior of Regular Expressions (g: Global; I: case-insensitive; m: multiline mode ).

Exec () is designed to capture arrays. It receives strings in the application mode and returns the first matching array. index returns the position and input returns the string.

Test (); checks whether the pattern matches this parameter. The valueof () method of the regular expression returns the regular expression itself.

5. Function Type

Function sum (num1, num2 ){

Return num1 + num2;

} // You can use varanothersum = sum; at this time, anothersum is the same as sum.

Var sum = function (num1, num2 ){

Return num1 + num2;

}; // A semicolon exists.

Alert (sum (10, 10 ));

Unction sum (num1, num2 ){

Return num1 + num2;

} // As shown above, the function declaration has been promoted to the top of the execution environment before the code is executed, so it is okay even after the code is executed. But the function expression cannot.

Alert (sum (10, 10 ));

Var sum = function (num1, num2 ){

Return num1 + num2;

}; // An error will occur during running

Function as Value

Function callSomeFunction (someFunction, someArgument ){

Return someFunction (someArgument );

} // That is, a function can be used as the value of another function, or a function can return the value of another function.

Internal Attributes of the function: arguments and this

You can use arguments. callee to remove the coupling between the code in the function body and the function name, instead of relying on a single function name.

Function Attributes and Methods: length (number of parameters the function wants to receive) and prototype (save all instance methods)

Apply (); call (); calling a function in a specific scope is actually equal to the value of this in a social self-function. The two are only different in the method of receiving parameters.

Function sum (num1, num2 ){

Feturn num1 + num2;

}

Function sum (num1, num2 ){

Return sum. apply (this, arguments );

// Return sum. call (this, num1, num2 );

 

} // Their biggest advantage is that the extended function depends on the running scope, and the object does not need to have any coupling relationship with the method. Bind () can also be bound to the running domain.

6. Basic packaging type

Boolean, Number, String reference type, but also has basic type behavior. When it exists as a reference type, it is automatically destroyed only when the code is executed.

7. Single built-in object

Objects implemented by ECMAScript that do not depend on the host environment already exist before ECMAScript is delicious. Developers do not need to explicitly instantiate built-in objects because they have already instantiated them. In addition to the Object, Array, String, Global, and Math.

 

 

Http://www.bkjia.com/kf/201412/359006.html previous

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.