JavaScript Note three: reference types

Source: Internet
Author: User

1. Object objects

Create Object objects,

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

Person.name = "Guoliang"; Name: "Guoliang",

Person.age = 20; Age:20

};

In addition, the property name can also use a string, and the second belongs to the object literal representation

Access object properties can use the "." or "[]", which can only be accessed using "[]" when the property name contains non-alphabetic and non-numeric. It is generally recommended to use ".".

2. Array type

The array in JavaScript differs from other languages in that it can hold data of any type, and its size is dynamically variable, increasing automatically as the data grows.

var color = new Array (); Creating a color array

or Var color=[];

var color = new Array (20); Create an array of length 20

var color = new Array ("Red", "Blue", "green");

or Var color= ["Red", "Blue", "green"];

New can be taken without the same result

You can get the length of the array using length, but not just read-only, length can also move the divisor or add new items at the end. And if you set a value that exceeds the current array size, the array recalculates the length value, which is equal to the last item's index value plus 1.

Array.isarray (); You can tell if the value is not an array.

Push and pop can be stacked and out-of-stack at the end of the array, while push (end) and shift (front-end) can be used for incoming and outbound operations. Unshift (front end) and pop (end) into the team and out of the team.

Sort by: Reverse () and sort (); they compare strings, but sort () appears 5>10 when compared, so use sort (compare (comparison function));

function Compare (value1,value2) {

return value2-value1; Ascending

}

Concat (); Connection array

Slice ();//Get the values in the array

Splice ();//delete/insert/replace

index ();//In the past

LastIndex ();//From Back forward

Every (); filter (); ForEach (); map (); some ();//Iterative method

Reduce (); Reduceright ();//Narrowing method

3. Data type

The number of milliseconds since midnight (0 o'clock) UTC January 1, 1970 to save the date.

Data.parse (); DATA.UTC ();D ata.now ();

Todatastring (); totimestring (); tolocaldatastring (); tolocaltimestring (); toutcstring ();

4. RegExp type

ECMAScript supports regular expressions through the regexp type.

var expression =/pattern/flags;

Patterns (pattern) allow arbitrary simple or complex regular expressions, flags (flags) to flag the behavior of regular Expressions (g: global; I: case insensitive; M: Multiline mode).

exec (); A string specifically designed for capturing an array, receiving an applied pattern, returning an array of the first occurrence information; index returns the position, input returns the string.

Test (); the valueof () method of the regular expression returns the regular expression itself, judging whether the pattern matches the parameter

5. Function Type

function sum (num1,num2) {

return num1+num2;

}//can use varanothersum=sum; At this point anothersum is the same as sum

var sum = function (num1,num2) {

return num1+num2;

};//has a semicolon

Alert (sum (10,10));

Unction sum (num1,num2) {

return num1+num2;

}//, the code has actually promoted the function declaration to the top of the execution environment before executing it, so even after the execution of the code. But the function expression is not possible.

Alert (sum (10,10));

var sum = function (num1,num2) {

return num1+num2;

};//An error occurs during run time

function as a value

function Callsomefunction (somefunction,someargument) {

Return someFunction (someargument);

}//that is, one function can be the value of another function, or one function can return the value of another function

Internal properties of the function: arguments and this

Using Arguments.callee, you can disassociate the code in the function body from the function name, no longer relying on a single function name.

Properties and methods of the function: Length (the number of arguments the function expects to receive) and prototype (where all instance methods are saved)

Apply (); call (); Calling a function in a particular scope is actually equal to the value of this in society from the function body. The two differ only in the way in which parameters are received.

function sum (num1,num2) {

Feturn num1+num2;

}

function sum (num1,num2) {

Return sum.apply (this,arguments);

Return Sum.call (THIS,NUM1,NUM2);

}//Their greatest benefit is the scope in which the function is to be run, and the object does not need to have any coupling with the method. Bind () can also bind the running domain.

6. Basic Packing Type

A Boolean, number, string reference type but also has a basic type of behavior. When it is present as a reference type, it is automatically destroyed only when the line code is executed.

7. Monomer built-in object

An object provided by the ECMAScript implementation that does not depend on the hosting environment, and these objects are already present before ECMAScript is good enough to execute. Developers do not have to instantiate the built-in objects explicitly because they are already instantiated. In addition to the object, Array, string and global and math mentioned.

JavaScript Note three: reference types

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.