js資料型別

2022-03-06 08:56:16 字數 4135 閱讀 4621

string、number、boolean、array、object、null、undefined

相同的變數可以用作不同的型別

var x                //

x 為 undefined

var x = 6; //

x 為數字

var x = "bill"; //

x 為字串

undefined 與 null

null即是乙個不存在的物件的佔位符

ecmascript認為undefined是從null派生出來的,所以把它們定義為相等的。

區分: 

concole.log(null === undefined);     //

false

concole.log(typeof

null == typeof undefined); //

false

ecmascript 中可用的 3 種強制型別轉換:boolean、number、string

型別結構

undefined

"undefined"

null

"object"(見下方)

布林值"boolean"

數值"number"

字串"string"

symbol (ecmascript 6 新增)

"symbol"

宿主物件(js環境提供的,比如瀏覽器)

implementation-dependent

函式物件 (implements [[call]] in ecma-262 terms)

"function"

任何其他物件

"object"

instanceof 的判斷方法是:

左側的原型鏈中的各個 [[prototype]] 指標是否指向 array.prototype(即右側物件的原型),如果有則返回 true。

instance:例項,例子,所以instanceof 用於判斷乙個變數是否某個物件的例項,是乙個三目表示式

instanceof 運算子用於識別正在處理的物件的型別,要求開發者明確地確認物件為某特定型別在使用

instanceof檢測變數型別時,我們是檢測不到number, 'string', bool, null, undefined  的型別的

var a =,

b = new

date(),

d ={},

e = null

;function

c(name)

alert(b

instanceof date) ----------------> true

alert(c

instanceof function) -------------> true

alert(c

instanceof

function) -------------> false

console.log(a

instanceof array) ----------> true

console.log(a

instanceof object)----------> true

console.log(c

instanceof array)-----------> false

console.log(e

instanceof object)----------> false

console.log(1 instanceof number)----------> false

//注意:instanceof 後面一定要是物件型別,並且大小寫不能錯,該方法適合一些條件選擇或分支。

function

type(item)

var itemtp = typeof

(item)

//if null

if (!item)

return itemtp !== 'object' ? itemtp : (item instanceof array) ? 'array' : (item instanceof 'object')

}

w3c定義:constructor 屬性返回對建立此物件的陣列函式的引用(返回物件對應的建構函式)

constructor本來是原型物件上的屬性,指向建構函式。但是根據例項物件尋找屬性的順序,若例項物件上沒有例項屬性或方法時,就去原型鏈上尋找,因此,例項物件也是能使用constructor屬性的

var a = new

array();

console.log(a

instanceof array) //

a是否array的例項 true

console.log(a.constructor == array) //

a例項所對應的建構函式是否為array true

//example

function

dog(name)

var dollar=new dog("dollar");

console.log(dollar.constructor);

//輸出function dog(name)

function

dog()

var tim = new

dog();

console.log(tom.constructor === dog);//

true

//constructor屬性是可以被修改的,會導致檢測出的結果不正確

function

dog()

function

cat()

cat.prototype = new

dog();

var m= new

cat();

console.log(m.constructor==cat); //

false

console.log(john.constructor==person); //

true

//instanceof 對於直接或間接引用都是true

console.log(m instanceof cat); //

true

console.log(john instanceof person); //

true

function

a() ;

var tostring =object.prototype.tostring;

console.log(tostring.call(

new date) === '[object date]'); //

true

console.log(tostring.call(new string) ==='[object string]');//

true

console.log(tostring.call(a) ==='[object function]'); //

true

jquery.type( undefined ) === "undefined"          //

true

jquery.type() === "undefined" //

true

jquery.type( null ) === "null" //

true

jquery.type( true ) === "boolean" //

true

陣列不是基礎型別

typeof  === 'object' //

true

要判斷乙個變數是否為陣列,需要用 array.isarray( var )

參考:

js資料型別

一.原始資料型別 1.typeof 運算子。var a alert typeof a 輸出結果為 undefined 2.undefined 未定義 型別 當變數未定義時,該變數值被預設為undefined 如 var a alert typeof a 輸出結果為 undefined 注意值unde...

js資料型別

js 基本資料型別 undefined null boolean number string js 操作符 typeof檢測給定變數的資料型別。返回值表示的型別 undefined 值未定義。boolean 值為布林型別。string 值為字串。number 值為數字。object 值為物件或nul...

js資料型別

var num 1,boo true,aa null,bb,str mary arr 1,2,4,8 obj arrnew new array 1,2,3 strnew new string 1,2,3 用 typeof 檢測變數的型別 console.log number typeof num n...