JavaScript學習筆記 資料型別

2021-07-11 14:54:22 字數 1688 閱讀 5334

一、資料型別

(一)、六種資料型別

原始型別: object number string boolean null undefined

object物件:functionarray date 

(二)、隱式轉換

1、== 和 ===

1.1  ==

"1.23"             ==    1.23

0                ==    false

null              ==    undefined

new object()  ==     new object()

[1,2]             ==     [2,1]

boolean         ==     ?

1.2 ===  嚴格等於

a  ===  b   型別和值都必須相同

但是 nan != nan          not a number

(三)、包裝物件

var str = "hello";                    //基本型別

var strobj = new string("hello");     //物件型別(包裝型別)

//當基本型別使用對應包裝型別的方法時,js會智慧型建立乙個對應的包裝型別副本,使用結束後銷毀

str.length;          //原則上str基本string型別,不能有屬性,但是此時建立了乙個包裝型別的副本

alert(str.length);   //得到6

str.t = 3;           //按照物件為其建立屬性   

alert(str.t);        //得到undefined

(四)、型別檢測

1.typeof

返回乙個字串,適合函式物件和基本型別的判斷

typeof 100      "number"

typeof true     "boolean"

typeof function "function"

typeof undefined "undefined"

typeof new object "object"

typeof [1,2]     "object"         

typeof nan       "number"

typeof null      "object"         ?????? null為乙個基本型別,但是由於歷史原因返回object

2.instanceof     適合自定義物件

[1,2] instanceof array  true

new object() instanceof array false

//不同windows或iframe之間的物件型別檢測

不能使用instanceof

3.object.prototype.tostring   適合內建物件和基本型別 遇到null 和 undefined失效

4.construtor//構造器、建構函式

5.duck type

JavaScript學習筆記 資料型別

number 數值 整數,浮點數 1,1.23 string 字串 hello boolean 布林 true,false object 物件 值的集合 可以分為 狹義的物件 陣列 function 函式 在oc中方法是不能作為資料型別的,但是swift中協議也可以作為資料型別 undefined ...

《javascript高階程式設計》筆記 陣列方法

join 能夠將陣列用指定的字元分割成字串 方法用法 變數的值 表示式的值 假設 arr 1,2,3 join arr.join 1,2,3 1,2,3 arr.join undefined 1,2,3 1,2,3 arr.join 1,2,3 1 2 3 arr.join null 1,2,3 1...

Javascript 學習筆記

如果在生成的html裡面有事件需要傳遞帶特殊字元的引數,處理如下 singletext 輸入 1.singletext value.escapehtml 為 將html編碼 2.singletext value.escapehtml inspect 為 3.在 jsdebugtext innerht...