JS type conversion and reference type detailed

Source: Internet
Author: User
Tags numeric valid hasownproperty

  This article is mainly to JS type conversion and reference type (boolean_number_string) for a detailed introduction, the need for friends can come to the reference, I hope to help you.

One, type conversion     1. The interesting thing about converting the Boolean value of a string   ECMAScript to the original values of numbers and strings is that they are pseudo objects, which means they actually have properties and methods.   such as:      code as follows: var scolor = "Blue"; alert (scolor.length);//outputs "4"   all in all, 3 of the main original values Boolean values, numbers, and strings have the ToString () method. All objects defined by ECMAScript have the ToString () method, whether it is a pseudo object or a true object. The     Boolean tostring () method simply outputs "true" or "false", and the result is determined by the value of the variable:      code is as follows: var bfound = false; Alert (bfound.tostring ());//outputs "false"   number type's toString () method is special, it has two modes, that is, default mode and base mode, default mode, ToString () Method simply outputs numeric values (whether integers, floating-point numbers, or scientific notation) with the corresponding string.     Code as follows: var iNum1 = 10; var fNum2 = 10.0; Alert (inum1.tostring ()); Outputs "ten" alert (fnum2.tostring ()); Outputs " " uses the base pattern of the number type ToString () method to output numbers in different bases (in radix).     Code as follows: var inum = 10; Alert (inum.tostring (2));  //outputs "1010" Alert (inum.tostring (8));  //outputs "a" alert (inum.tostring (16)); Outputs "A"   2. Converting to digital   ECMAScript provides two methods for converting non-numeric raw values to numbers, namely parseint () and parsefloat ().   Note: Only for string type (except number)Call these methods to correctly run Nan for all other types returned.     For example:      code is as follows: var iNum1 = parseint ("1234blue");//returns 1234 var iNum2 = parseint ("Oxa"); returns var iNum3 = parseint ("22.5"); returns var iNum4 = parseint ("Blue"); The returns NaN   parseint () method also has a base pattern that converts binary, octal, hexadecimal, or any other string of strings into decimal integers. The second parameter specifies which system to parse.     Code as follows: var iNum1 = parseint ("AF");//returns 175 var iNum2 = parseint ("10", 2); Returns 2 var iNum3 = parseint ("10", 8); Returns 8 var iNum4 = parseint ("10", 10); Returns   Description: If the decimal number contains a leading 0, it is best to use cardinality 10, otherwise the octal value is obtained.   Code as follows: var iNum1 = parseint ("010");  //returns 8 var iNum2 = parseint ("010", 8); Returns 8 var iNum3 = parseint ("010"),//returns the   parsefloat () method is similar to the parseint () method, starting from position 0 to view each character, Until you find the first character that is not valid, and then convert the string before the character to a number. For this method, the first decimal point that appears is a valid character. If you use two decimal points, the second decimal point is considered invalid. Another difference in using this method is that the string must represent a floating-point number in decimal form.     Code as follows: var fNum1 = parsefloat ("1234blue"); Returns 1234.0 var fNum2 = parsefloat ("0xA"); Returns NaN var fNUM3 = parsefloat ("22.5"); Returns 22.5 var fNum4 = parsefloat ("22.34.5");//returns 22.34 var fNum5 = parsefloat ("0908");//returns NaN var fNum6 = Parsefloat ("Blue");//returns NaN   3. The 3 types of coercion available in the coercion type conversion   ECMAScript are as follows:    (1). Boolean (value)   Converts the given value to a Boolean.   the Boolean () function returns True when the value to be converted is a string of at least one character, a number other than 0 digits, or an object. If the value is an empty string, number 0, undefined, or null, it returns FALSE.   such as:      code as follows: var B1 = Boolean (""); False var B2 = boolean ("Hi");//true var b3 = Boolean (m);//true var b4 = boolean (null);//false var b5 = boolean (0);//false var B6 = Boolean (New Object ());//true   (2). Number (value)   Converts the given value to numbers (which can be integers or floating points).   Remember that the parseint () and parsefloat () methods only convert the string before the first invalid character, so "4.5.6" will be converted to "4.5". Force type conversion with number (), and "4.5.6" returns Nan, because the entire string value cannot be converted to numbers. If the string can be completely converted, number () will determine whether to call the parseint () method or call the Parsefloat () method.   such as:    code below: Number (false),//0 number (true),//1 number (undefined),//nan number (null),//0 number ("5.5") //5.5 number ("//56"), Number ("5.6.7"),//nan NumbER (new Object ());//nan number (m);//100   (3). String (value)   Converts the given value to a string. The only difference between   and invoking the ToString () method is that coercion of type conversions on null or undefined values can generate strings without raising errors:    code as follows: var S1 = string (null);//null "var onull = null; var s2 = onull.tostring ();//causes An error   Two, reference type   reference type is usually called class, that is, the object is handled when the reference value is encountered. ECMAScript defines an "object definition" that is logically equivalent to a class in another programming language. All classes in     1.Object   ECMAScript are inherited by this class, and all properties and methods in the object class appear in other classes (overwritten).     Properties of the object class:    (1). constructor----A reference (pointer) to the function that created the object. For the object class, the pointer points to the original object () function.     (2). Prototype----A reference to the object's prototype of the object. For all classes, it returns an instance of Object objects by default.     Method of object class:    (1). The hasOwnProperty (property)----Determine whether an object has a specific attribute. This property must be specified with a string (for example: O.hasownproperty ("name")).     (2). isPrototypeOf (object)----Determine whether the object is a prototype of another object.     (3). propertyIsEnumerable----Determine whether a given property can be used for ... In statement to enumerate.     (4). ToString ()----Returns the original string representation of an object. Different ECMAScript implementations have different values.     (5). valueof ()----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   The use of Boolean objects is rarely used in ECMAScript, and is difficult to understand even when used.   For example:      code is as follows: Var ofalseobject  = new Boolean (false); var bresult = ofalseobject  && true;//outputs  true;   Reason: In a Boolean expression, all objects are automatically converted to true.     3.Number class   Number.MAX_VALUE and so on special values are the static properties of the number class. To get number raw values for a numeric object, you only need to use the valueof () method:  var inumber = onumberobject.valueof ();  in addition to the standard methods inherited from the object class, The number class also has several special methods for handling numeric values. The     toFixed () method:  Returns a string representation of a number with a specified number of decimal digits. Method can represent a number with 0 to 20 decimal digits, and a value that is outside this range throws an error.   such as:      code as follows: var onumberobject = new number (99); Aler (onumberobject.tofixed (2));//outputs "99.00"   toexponential () method:  returns the string form of a number represented by scientific notation. The method also has a parameter that specifies the number of decimal places to output. For example: The   code is as follows: var onumberobj = new number (99); Alert (onumberobj.toexponential (1));//outputs "9.9e+1"   toprecision () method:  returns the predetermined form or exponential form of a number according to the most meaningful form. It has a parameter, which is the total number of digits (excluding exponents) that are used to represent the number.     Code as follows: var onumberobj = new number (99); AlerT (onumberobj.toprecision (1));//outputs "1e+2" ==100   It can be seen that the toprecision () method rounds the logarithm to get as close to the actual value as possible.   such as:   code as follows: var onumberobj = new number (99); Alert (Onumberobj.toprecision (2));//Outputs "" "Alert (Onumberobj.toprecision (3));//outputs" 99.0 "  toFixed (), The toexponential () and Toprecision () methods are rounded to correctly represent a number with the correct number of decimal digits. The     tolocalestring () method:  can be displayed on the page, such as 5210.50 displayed as 5,210.50, but if you use its value, you should use Parsefloat ($ ("N_yjje"). Value.replace (//,/g, "")), replace the comma by the form, and get its value.     NOTE: Similar to a Boolean object, the number object is also important, but you should use this object sparingly to avoid potential problems. Use the original notation of numbers whenever possible. Both the valueof () method and the ToString () method of the     4.String   String Object return the original value of string:    code as follows: Alert ( ostringobj.valueof () = = Ostringobj.tostring ());//outputs "true"   String class has attribute length, which is the number of characters in a string:    The code is as follows: var ostringobj = new String ("Hello World"); alert (ostringobj.length); outputs   NOTE: Even if the string contains double-byte characters, each character is counted as a single character. The     charAt () method:  returns a string containing the character at the specified position:    code as follows: var ostringobj = new String ("Hello World"); Alert (Ostringobj.charat (1)); outputs the "E"   charCodeAt () method:  returns a string:    code containing the character code at the specified location as follows: Var Ostringobj = new String ("Hello World"); Alert (Ostringobj.charcodeat (1)); outputs "  concat () method:  is used to connect one or more strings to the original values of a string object. The original string object does not change. The   code is as follows: var ostringobj = new String ("Hello"); var sresult = Ostringobj.concat ("World"),//ostringobj+ "World", more common alert (sresult),//outputs "Hello World" alert ( Ostringobj); the//outputs "Hello"   indexOf () and LastIndexOf () methods return the position of the specified substring in another string (or-1 if the substring is not found). The differences between the two methods are greater than the indexOf (), which retrieves the substring starting at the beginning of the string (position 0), while LastIndexOf () retrieves the substring from the end of the string.     Localecompare () to compare strings (in alphabetical order, the larger the later). The method has one argument-the string to compare, and returns one of the following 3 values:  1. Returns a negative number (most commonly-1, although the real return is determined by the implementation) if the string object is in alphabetical order in the argument.   2. If the string object equals the strings in the argument, returns 0.   3. If the string object is in alphabetical order after the strings in the argument, returns a positive number (most commonly 1, although the true return is determined by the implementation)     slice () and substring () methods:  Both methods return a substring of the string to be processed, accepting one or two arguments. The first argument is the starting position of the substring to get, and the second parameter is the position before the substring terminates (the character at the end position does not include the value that is returned in the large return). If you omit paragraphTwo parameters, the termination bit defaults to the length of the string. Neither of these methods changes the value of the string object itself.       Code as follows: var ostringobj = new String ("Hello World"); Alert (Ostringobj.slice (3));//outputs "Lo World" alert (Ostringobj.slice (3,7))//outputs "Lo w"   Note: For negative arguments, slice ( Method uses the length of the string, and the substring () method will treat it as 0 (that is, it will be ignored). The   code is as follows: var ostringobj = new String ("Hello World"); Alert (Ostringobj.slice ( -3));//outputs "Rld" is equivalent to reverse fetch alert (ostringobj.substring ( -3))//outputs "Hello World" alert ( Ostringobj.slice (3,-4));//outputs "Lo w" alert (ostringobj.substring (3,-4));//outputs "hel" substring () always takes the smaller number as the starting bit , the larger number as the termination bit.   toLowerCase (), Tolocallowercase (), toUpperCase (), and toLocaleUpperCase ():  the first two ways to convert strings to lowercase. The latter two methods are used to convert the string to uppercase. The Tolocallowercase () and toLocaleUpperCase () methods are implemented based on a specific region.     Remember that all properties and methods of the string class can be applied to string raw values because they are pseudo objects.     5.instanceof operator   when using the TypeOf operator to store values with a reference type, a problem arises, regardless of what type of object is referenced, it returns "Object". The Instanceof method shows that the developer explicitly confirms that the object is a specific type. such as:      code as follows: var ostrobj = new String ("Hello World");  &nbSp Alert (ostrobj instanceof String);//outputs "true"      

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.