Javascript series: ecmascript reference type

Source: Internet
Author: User
Tags define prototype hasownproperty

The reference type is usually called a class, and the reference value is processed as an object. An object is created with the new operator and the name of the class to be instantiated. When there is more than one parameter, ecmascript requires brackets. If there is no parameter, parentheses can be omitted.

 
VaRO1 =NewObject ();VaRO2 =NewObject;

Although parentheses are not mandatory, brackets are recommended to avoid weddings.

 

1. Object Class

All classes in ecmascript are inherited from the object class, And all attributes and methods in the object class will appear in other classes.

The object class has attributes.:

Extends constructor -- reference (pointer) of the function for creating an object ). For object classes, this pointer points to the original object () function.

Define prototype -- reference the object prototype of the object.

Methods of the object class:

◊ Hasownproperty (property) -- determines whether an object has a specific property. You must use a string to specify this property (for example, O. hasownproperty ("name ")).

◊ Isprototypeof (object) -- determines whether the object is a prototype of another object.

Define prototypeisenumerable (property) -- determines whether a given property can be used for enumeration in the for. In statement.

Returns the original string representation of the object. The ECMA-262 does not define this value for the object class, so different ecmascript implementations have different values.

Incluvalueof () -- returns the original value that best fits the object. For many classes, the value returned by this method is the same as the return value of tostring.

 

2. boolean class

The Boolean class is a reference type of the Boolean primitive type. To create a Boolean object, you only need to pass the Boolean value as the parameter:

 
VaRObooleanobject =NewBoolean (True);

The Boolean object will override the valueof () method of the object class and return the original value, that is, true or false. Override tostring () to return the "true" and "false" strings ".

 

3. Number

The number class is a reference type of the original number type. Create a number objectCodeAs follows:

VaROnumberobject =NewNumber (55 );

Use the valueof () method to obtain the original numeric object number value:

 
VaRInumber = onumberobject. valueof ();

The number class is used to process numeric values:

BytesTofixed ()-- Returns the string representation of digits with the specified decimal digits.

 
VaROnumberobject =NewNumber (99); Alert (onumberobject. tofixed (2 ));//Outputs "99.00"

The tofixed () method parameter specifies the number of decimal places to be displayed. The number of empty decimal places is supplemented by 0. The tofixed () method can represent numbers with 0 to 20 decimal places. An error occurs if the value exceeds this range.

BytesToexponential ()-- Returns a string of numbers expressed in scientific notation.

BytesToprecision ()-- Returns the predefined or exponential form of a number.

 

4. string type

The string class is an object representation of the original string type. Create a String object:

 
VaROstringobject =NewString ('Hello World ');

Both the valueof () and tostring () Methods of the string object return the original values of the string type:

 
Alert (ostringobject. valueof () = ostringobject. tostring ());//Outputs "true"

String attributes:

◊ Length, Returns the number of characters in the string;

 
VaROstringobject =NewString ('Hello world'); Alert (ostringobject. Length );//Outputs "11"

String method:

BytesCharat ()-- Returns a string containing characters at the specified position. The first character is 0, the second character is 1, and so on.

VaROstringobject =NewString ("Hello World"); Alert (ostringobject. charat (1 ));//Outputs "e"

BytesCharcodeat ()-- Return the code that contains characters at the specified position. The first character is 0, the second character is 1, and so on.

 
VaROstringobject =NewString ("Hello World"); Alert (ostringobject. charcodeat (1 ));//Outputs "101"

BytesConcat ()-- Connects one or more strings to the original value of the string object. This method returns the original string value, keeping the original string object unchanged.

VaROstringobject =NewString ("hello");VaRSresult = ostringobject. Concat ("world"); Alert (sresult );//Outputs "Hello World"Alert (ostringobject );//Outputs "hello"

BytesIndexof ()-- Retrieves the position of a specified substring in another string starting with a string. If this substring is not found,-1 is returned.

BytesLastindexof ()-- Retrieves the position of the specified substring in another string from the end of the string. If this substring is not found,-1 is returned.

VaROstringobject =NewString ("Hello World"); Alert (ostringobject. indexof ("0 "));//Outputs "4"Alert (ostringobject. lastindexof ("0 "));//Outputs "7"

BytesSlice ()AndSubstring ()-- Both return the substring of the string to be processed, and both accept one active parameter. The first parameter is the starting position of the substring to be obtained, and the second parameter is the position before the substring is terminated. If the second parameter is omitted, the ending position is the string length by default.

When the slice () and substring () parameters are negative, the processing method is different. For negative parameters, the slice () method adds a parameter to the length of the string, and the substring () method treats it as 0, that is, the negative number is ignored.

BytesTolowercase ()-- Converts a string to lowercase.

BytesTouppercase ()-- Converts a string into uppercase.

 

5. instanceof Operator

The instanceof operator is similar to the typeof operator and is used to identify the type of the object being processed. Unlike the typeof method, the instanceof method requires developers to explicitly confirm that the object is of a specific type.

 
VaROstringobject =NewString ('Hello world'); Alert (ostringobjectInstanceofString );//Outputs "true"

6. array Array

String, value, and boolean values are discrete values. If a variable is discrete, only one value can be set at any time. If you want to use a variable to store a group of values, you need to use an array.

In JavaScript scripts, arrays are declared using the keyword array. You can also specify the number of elements in the array, that is, the length of the array ).

 
VaRAteam =NewArray (12 );

When you cannot predict the final number of an array element in advance, you can specify no specific number when declaring an array.

VaRAcolor =NewArray (); acolor [0] = "red"; Acolor [1] = "blue"; Acolor [2] = "Purple ";

Each time an array item is added, the length of the array increases dynamically.

You can also directly create an array with parameters.

 
VaRAMAP =NewArray {"China", "USA "};

Like the Length attribute of a string, the Length attribute can be used to obtain the length of an array. The position of the array starts from 0.

In addition to array () objects, Arrays can also be defined using square brackets "[" and "]". items and items are separated by commas.

 
VaRAcolor = ["red", "gray"];

For an array, you usually need to convert it into a string before using it. The tostring () method can easily implement this function.

 
Alert (acolor. tostring ());

If you do not want to use a comma for the converted connection, but want to use the specified symbol, you can use the join () method. This method accepts a parameter, that is, the string used to connect the array items.

VaRScolor = acolor. Join ("| ");

 

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.