1. 基本データ型: 数値、文字列、ブール値
2. 特別なデータ型: null、未定義 (違いは、null は明示的な代入が必要であるのに対し、未定義は代入がないことを意味します)
3. 複合 (参照) ) データ型: オブジェクト (配列は特別なオブジェクトです)
ただし、複合データ型(関数を除く)の場合は、typeof オブジェクトが別のオブジェクトのインスタンスであるかどうかを検出するには、instanceof を使用できます。
return Object.prototype.toString. call(o) === '[オブジェクト配列]';
実際、これら 2 つの関数はほぼ同じです。call(thisObj[,arg1[, arg2[,)) の arg パラメータは変数にできるのに対し、apply([thisObj[ ,argArray]] ) は配列コレクションです
'未定義' : '未定義',
'数値' : '数値',
'ブール' : 'ブール',
'文字列' : '文字列',
'[オブジェクト関数]' : '関数'、
'[オブジェクト RegExp]' : '正規表現'、
'[オブジェクト配列]' : '配列'、
'[オブジェクト日付] ' : '日付',
'[オブジェクト エラー]' : 'エラー'
}
関数 type(o) {
return _types[typeof o] || .call(o)] || ( o ? 'オブジェクト' : 'null');
4. Data type conversion Javascript has two data type conversion methods:
One is to convert the entire value from one type to another data type (called Basic data type conversion),
Another method is to extract a value of another type from one value and complete the conversion work.
There are three basic data type conversion methods:
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 type: Boolean(); Example: The result of Boolean("aaa") is true
Extract from a value Another type of value is as follows:
1. Extract the integer in the string: parseInt(); For example: the result of parseInt("123zhang") is 123
2. Extract the float in the string Points: parseFloat(); Example: The result of parseFloat("0.55zhang") is 0.55
In addition, summarize the methods of various type conversions
Number is converted into a string :String(number),
Number.toString(2/8/16);//Represent binary, octal, hexadecimal, default (no parameters) decimal,
toFixed(3) //Retain decimal point Last 3 digits
toExponextial(3); // 1 digit before the decimal point, 3 digits after the decimal point such as var n=123456.789; n.toExponextial(3); //1.234e 5 is 1.234X105
toPrecision(4) ; //Return the specified number of digits. If the number of digits is not fully displayed, use exponential notation (all three methods will round 4 to 5)
5. Other summary (things that are easily overlooked)
1. The pitfalls of parseInt
The following part is excerpted from "Javascript Essence":
parseInt is a function that converts a string into an integer. It stops parsing when it encounters a non-number, so parseInt("16") produces the same result as parseInt("16 tons"). It would be nice if the function informed us that extra text is present, but it doesn't do that.
If the first character of the string is 0, then the string will be evaluated based on octal rather than decimal. In octal, 8 and 9 are not the same number, so parseInt("08") and parseInt("09") produce 0 as a result. This bug causes problems when the program parses dates and times. Fortunately, parseInt can accept a radix as an argument, so parseInt("08",10) returns 8. I recommend that you always provide this radix argument.
Also. The following will display 1:
alert(parseInt(0.0000001));
This is because js will use scientific notation to record numbers when it exceeds a certain precision, for example:
alert(0.0000001 );
will get 1e-7, and parseInt will automatically convert the parameter into a string, which is actually:
s = (0.0000001).toString();
alert(parseInt(s));
Finally It's not surprising to get 1.
When using parseInt, you must remember that the parameters are converted into strings and then converted.