1/**2對陣列型別的判斷3*/
45var n=1,
6 s='hello',
7 b=true
,8 un=undefined,
9 nu=null
,10 fun=function
() {};
11 console.log(n+'型別是:'+typeof
n);12 console.log(b+'型別是:'+typeof
b);13 console.log(un+'型別是:'+typeof
un);
14 console.log(nu+'型別是:'+typeof
nu);
15 console.log(fun+'型別是:'+typeof
fun);
16/*
171型別是:number
18true型別是:boolean
19undefined型別是:undefined
20null型別是:object
21function () {}型別是:function
22*
*/23
24/*
25typeof 只能用於區分5種原始型別(number\string\boolean\null\undefined)和函式function,
26無法用於區分內建物件型別
27*/
2829
/*30
* 原型物件:集中儲存同一型別的子物件所需的所有共有屬性和方法的父物件。在定義建構函式的時候自動建立原型物件。
31* 建構函式型別名.prototype == 父型別原型物件 == 子型別原型物件.__proto__
32*
*/33
34var obj1=[1,2,3],
35 obj2={},
36 obj3={},
37 obj4=function
() {},
38 obj5={};
3940 obj3.__proto__ =array.prototype;
41 obj5.__proto__ = obj1; //
obj5-->obj1-->array.prototype
4243 console.log('obj1型別是: '+typeof
obj1);
44 console.log('obj2型別是: '+typeof
obj2);
45 console.log('obj4型別是: '+typeof
obj4);
4647 console.log('obj3型別是: '+typeof
obj3);
48 console.log('obj5型別是: '+typeof
obj5);
49/*
50obj1型別是: object
51obj2型別是: object
52obj4型別是: function
5354
obj3型別是: object
55obj5型別是: object
56*
*/57/**
584種用於判斷陣列型別的方法:
59第一種:var bool = father.isprototypeof(child)---該方法判斷比較鬆散
60第二種:var bool = child instanceof array---該方法判斷比較鬆散
61第三種:判斷物件內部屬性class---該方法判斷比較嚴格
62第四種:es5: array.isarray(obj)----該方法判斷比較嚴格
63**/64//
第一種:var bool = father.isprototypeof(child)
65console.log(
66array.prototype.isprototypeof(obj1),
67array.prototype.isprototypeof(obj2),
68array.prototype.isprototypeof(obj3),
69array.prototype.isprototypeof(obj4),
70array.prototype.isprototypeof(obj5),
71);
72//
result:true false true false true
7374
75//
第二種:var bool = child instanceof array
76console.log(
77 obj1 instanceof
array,
78 obj2 instanceof
array,
79 obj3 instanceof
array,
80 obj4 instanceof
array,
81 obj5 instanceof
array,
82);
83//
result:true false true false true
8485
//第三種:判斷物件內部屬性class
86console.log(
87 object.prototype.tostring.call(obj1) == '[object array]',
88 object.prototype.tostring.call(obj2) == '[object array]',
89 object.prototype.tostring.call(obj3) == '[object array]',
90 object.prototype.tostring.call(obj4) == '[object array]',
91 object.prototype.tostring.call(obj5) == '[object array]'
92);
93//
result:true false false false false
9495
//第四種:es5: array.isarray(obj)
96console.log(
97array.isarray(obj1),
98array.isarray(obj2),
99array.isarray(obj3),
100array.isarray(obj4),
101array.isarray(obj5),
102);
103//
result:true false false false false
104
JS判斷資料型別的4種方法
4種判斷方法分別是 可判斷的型別對比如下圖 建構函式名方法 function getconstructorname data 物件原型方法 不能判斷自定義函式物件型別 function getprototypename data 自定義的建構函式 function func var newobj n...
判斷素數的4種方法(C語言)
判斷素數的4種方法素數定義 質數又稱素數。指整數在乙個大於1的自然數中,除了1和此整數自身外,沒法被其他自然數整除的數。換句話說,只有兩個正因數 1和自己 的自然數即為素數。比1大但不是素數的數稱為合數,1和0既非素數也非合數。素數在數論中有著很重要的作用。一言不合直接上 include inclu...
判斷物件型別的三種方法
a instanceof b 判斷a物件是不是b構造出來的 判斷a物件的原型鏈上有木有b function person var person new person var obj console.log person instanceof person true console.log perso...