JavaScript Learning notes One of the data types _ basic knowledge

Source: Internet
Author: User
Tags wrapper
I. Data types
JavaScript is a weak type of scripting language that has 6 data types and is divided into basic data types, special data types, and composite data types.
1. Basic data type: Numeric type, String type, Boolean type
2. Special Data type: null,undefined (the difference is that NULL requires an explicit assignment, while undefined means no assignment)
3. Composite (Reference) data type: Object (array is a special object)

Note: Understand the difference between the underlying data type and the reference data type. such as function parameter passing

two. Relationship between wrapper class and underlying data type
For the underlying data type, there are corresponding wrapper classes (object objects) corresponding to them.
Number,string,boolean

Note: The underlying data type is converted to the underlying type wrapper object under certain conditions
Copy Code code as follows:

var str= "This is a base string";
var length=str.length ();
When you use Length (), the JavaScript interpretation engine produces
A temporary string object for STR, which clears the temporary object after execution

Three. How to determine the data type
(1) typeof (Chicken ribs)
Only the following 6 types of data can be detected: number, String, Boolean, undefined, object, function (Note!). )
Copy Code code as follows:

Alert (typeof (null)); Result is Object
Alert (typeof (a)); A is not assigned a value and the result is undefined

Therefore, the underlying data types can be judged as follows:
Copy Code code as follows:

function Type (o) {
return (o = = null)? ' null ': typeof (O);
}

(2) instanceof
But for composite data types (except function), all returns object and cannot be judged by typeof
You can use instanceof to detect that an object is not an instance of another object, and note that the right-hand operand of instanceof must be an object:
Copy Code code as follows:

function Animal () {};
function Pig () {};
Pig.prototype = new Animal ();
Alert (new Pig () instanceof Animal); True

Instanceof is not suitable to detect the type of an object itself

(3) constructor
Copy Code code as follows:

alert (1.constructor); Error
var o = 1;
alert (o.constructor); Number
o = null; or undefined
alert (o.constructor); Error
Alert ({}.constructor); Object
alert (true.constructor); Boolean

(4) object.tostring ()
Copy Code code as follows:

function IsArray (o) {
return Object.prototype.toString.call (o) = = ' [Object Array] ';
}


The difference between call and apply:
They are all function.prototype methods (for methods), which are implemented internally by the JavaScript engine.
In fact, the two functions are almost identical, and note that the ARG parameter in call (thisobj[,arg1[, arg2[,) can be a variable, and the parameters in apply ([Thisobj[,argarray]]) are set to an array
The method is to lend a call to another object to complete the task, in principle the context object changes when the method executes.

(5) Summary
Copy Code code as follows:

var _tos = Object.prototype.toString,
_types = {
' undefined ': ' Undefined ',
' Number ': ' Number ',
' Boolean ': ' Boolean ',
' String ': ' String ',
' [Object function] ': ' function ',
' [Object RegExp] ': ' RegExp ',
' [Object Array] ': ' Array ',
' [Object Date] ': ' Date ',
' [Object ERROR] ': ' Error '
};

function Type (o) {
Return _types[typeof O] | | _types[_tos.call (o)] | | (O?) ' object ': ' null ');
}

four. Data type conversion
JavaScript has two conversion methods for data types:
One is to convert the entire value from one type to another (called a basic data type conversion).
Another method is to extract the value of another type from one value and complete the conversion.
The basic data types are converted in the following three ways:
1. Convert to Character type: String (); Example: The result of String (678) is "678"
2. Convert to Numeric type: number (); Example: The result of number ("678") is 678
3. Convert to Boolean: Boolean (); Example: Boolean ("AAA") results are true
The following methods are used to extract values from a value of another type:
1. Extract the integer in the string: parseint (); Example: The result of parseint ("123zhang") is 123
2. Extract the floating-point number in the string: parsefloat (); Example: The result of parsefloat ("0.55zhang") is 0.55

  
In addition, summarize various types of conversion methods

  
Number is converted to a string: string (number),
Number.tostring (2/8/16);//represents binary \ octal \ Hexadecimal default (no parameter) 10,
ToFixed (3)//3 digits after the decimal point
Toexponextial (3); 1 digits before decimal point, 3 digits after decimal point such as Var n=123456.789; N.toexponextial (3);//1.234e+5 is 1.234x105
Toprecision (4); Returns the specified number of digits if the number of digits is not fully displayed, the index notation (3 methods will be 4 homes 5)
Five. Other summary (things that are easily overlooked)
1.parseInt traps.
The following sections are excerpted from the JavaScript Essence:
Parseint is a function that converts a string to an integer. It encounters a non-numeric stop parsing, so parseint ("16") and parseint ("tons") produce the same result. It would be nice if the function prompts us for extra text, but it won't do that.
If the first character of the string characters 0, the string is evaluated based on the octal rather than the decimal value. In the octal system, 8 and 9 are not a number, so parseint ("08") and parseint ("09") produce 0 as the result. This error causes the program to parse the date and time when the problem occurs. Fortunately, parseint can accept a cardinality as an argument, so that parseint ("08", 10) results in 8. I suggest you always provide this cardinal parameter.
Other than that. This will show 1:
  
Alert (parseint (0.0000001));
This is because more than a certain degree of precision JS will use scientific notation to record numbers, for example:
  
alert (0.0000001);
Will get 1e-7, and parseint will automatically convert the argument to a string, which is actually:
  
Copy Code code as follows:

s = (0.0000001). toString ();
Alert (parseint (s));

It's no surprise to get 1 in the end.
Using parseint you must remember that the parameters inside are converted to strings and then converted.

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.