Javascript学習ノート1 データ型_基礎知識

WBOY
リリース: 2016-05-16 18:13:43
オリジナル
928 人が閲覧しました
1. データ型
JavaScript は弱い型指定のスクリプト言語であり、基本データ型、特殊データ型、複合データ型に分類される合計 6 つのデータ型があります。
1. 基本データ型: 数値、文字列、ブール値
2. 特別なデータ型: null、未定義 (違いは、null は明示的な代入が必要であるのに対し、未定義は代入がないことを意味します)
3. 複合 (参照) ) データ型: オブジェクト (配列は特別なオブジェクトです)

注: 基本データ型と参照データ型の違いを理解してください。

を渡す関数のパラメータなど 2. パッケージングクラスと基本データ型の関係
基本データ型には、それに対応したパッケージングクラス(Objectオブジェクト)が存在します。
数値、文字列、ブール値

注: 基本データ型は、特定の条件下で基本型パッケージング オブジェクトに変換されます
コードをコピー コードは次のとおりです。

var str="これは基本的な文字列です";
var length=str.length(); length() を使用すると、JavaScript 解釈エンジンは
//str の一時 String オブジェクトを生成します。実行後、一時オブジェクトはクリアされます


3.データ型
(1) typeof (tasteless)
次の 6 つのデータ型のみを検出できます: 数値、文字列、ブール、未定義、オブジェクト、関数 (注!)

コードをコピー コードは次のとおりです。
alert(typeof(null)) //結果は object
alert(typeof(a)); // a が割り当てられていないため、結果は未定義です

したがって、基本的なデータ型は次のように決定できます:

コードをコピー コードは次のとおりです。
function type(o) {
return (o === null) ? 'null' : typeof(o);
}

(2)instanceof
ただし、複合データ型(関数を除く)の場合は、typeof オブジェクトが別のオブジェクトのインスタンスであるかどうかを検出するには、instanceof を使用できます。


Copy のように、instanceof の右側がオブジェクトである必要があります。コード コードは次のとおりです:
function Animal() {} ;
function Pig() {};
Pig.prototype = new Animal();
alert(new Pig() instantof Animal); // true


オブジェクト自体のタイプの検出には適していません

(3)コンストラクター


コードをコピーします コードは次のとおりです。
alert(1.constructor) / / エラー報告
var o = 1;
alert(o.constructor); // 数値
o = null; // エラー報告
alert({}.constructor); // オブジェクト
alert(true.constructor); // ブール値


(4 ) Object.toString()


コードをコピーします コードは次のとおりです: function isArray(o) {
return Object.prototype.toString. call(o) === '[オブジェクト配列]';
}



call と apply の違い:
これらはすべて Function.prototype (メソッドの場合)、JavaScript エンジンによって内部的に実装されます。
実際、これら 2 つの関数はほぼ同じです。call(thisObj[,arg1[, arg2[,)) の arg パラメータは変数にできるのに対し、apply([thisObj[ ,argArray]] ) は配列コレクションです
メソッドはタスクを完了するために別のオブジェクトの呼び出しに貸し出されます。

(5) 概要



コードをコピーします コードは次のとおりです。 var _toS = Object.prototype.toString,
_types = {
'未定義' : '未定義',
'数値' : '数値',
'ブール' : 'ブール',
'文字列' : '文字列',
'[オブジェクト関数]' : '関数'、
'[オブジェクト 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:
 
Copy code The code is as follows:

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.
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!