1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="utf-8 ">
5+ </ head >
6+ < body >
7+ < script >
8+ // typeof、instanceof、constructor、prototype、$.type()/jquery.type()
9+ var a = "csxiaoyao" ;
10+ var b = 1993 ;
11+ var c = true ;
12+ var d = undefined ;
13+ var e = null ;
14+ var f = new Date ( ) ;
15+ var g = [ 1 , 2 , 3 ] ;
16+ var h = function ( ) { alert ( "sunshine" ) ; } ;
17+
18+ // 1. typeof (返回的类型为字符串形式)
19+ console . log ( typeof a ) ; // string
20+ console . log ( typeof b ) ; // number
21+ console . log ( typeof c ) ; // boolean
22+ console . log ( typeof d ) ; // undefined
23+ console . log ( typeof e ) ; // object
24+ console . log ( typeof f ) ; // object
25+ console . log ( typeof g ) ; // object
26+ console . log ( typeof h ) ; // function
27+ console . log ( typeof a == "string" ) ; // true
28+ console . log ( typeof a == String ) ; // false
29+
30+ // 2. instanceof (判断已知对象类型的方法)
31+ // console.log(e instanceof null); // Right-hand side of 'instanceof' is not an object
32+ console . log ( f instanceof Date ) ; // true
33+ console . log ( g instanceof Array ) ; // true
34+ console . log ( h instanceof Function ) ; // true
35+
36+ // 3. constructor (根据对象的constructor判断)
37+ console . log ( f . constructor === Date ) ; // true
38+ console . log ( g . constructor === Array ) ; // true
39+ console . log ( h . constructor === Function ) ; // true
40+ // 注意:constructor 在类继承时会出错
41+ function A ( ) { } ;
42+ function B ( ) { } ;
43+ A . prototype = new B ( ) ; //A继承自B
44+ var aObj = new A ( ) ;
45+ console . log ( aObj . constructor === A ) ; // false
46+ console . log ( aObj . constructor === B ) ; // true
47+ // instanceof没有问题
48+ console . log ( aObj instanceof A ) ; // true
49+ console . log ( aObj instanceof B ) ; // true
50+ // 解决construtor的问题通常让对象的constructor手动指向自己
51+ aObj . constructor = A ; //将自己的类赋值给对象的constructor属性
52+ console . log ( aObj . constructor === A ) ; // true
53+ console . log ( aObj . constructor === B ) ; // false
54+
55+ // 4. prototype(通用但很繁琐的方法)
56+ console . log ( Object . prototype . toString . call ( a ) === '[object String]' ) // true
57+ console . log ( Object . prototype . toString . call ( b ) === '[object Number]' ) // true
58+ console . log ( Object . prototype . toString . call ( c ) === '[object Boolean]' ) // true
59+ console . log ( Object . prototype . toString . call ( d ) === '[object Undefined]' ) // true
60+ console . log ( Object . prototype . toString . call ( e ) === '[object Null]' ) // true
61+ console . log ( Object . prototype . toString . call ( f ) === '[object Date]' ) // true
62+ console . log ( Object . prototype . toString . call ( g ) === '[object Array]' ) // true
63+ console . log ( Object . prototype . toString . call ( h ) === '[object Function]' ) // true
64+ // 大小写不能写错,比较麻烦,但胜在通用
65+
66+ // 5. jquery.type() (万能方法)
67+ // 如果对象是undefined或null,则返回相应的“undefined”或“null”
68+ jQuery . type ( undefined ) === "undefined"
69+ jQuery . type ( ) === "undefined"
70+ jQuery . type ( window . notDefined ) === "undefined"
71+ jQuery . type ( null ) === "null"
72+ // 如果对象有一个内部的[[Class]]和一个浏览器的内置对象的 [[Class]] 相同,返回相应的 [[Class]] 名字
73+ jQuery . type ( true ) === "boolean"
74+ jQuery . type ( 3 ) === "number"
75+ jQuery . type ( "test" ) === "string"
76+ jQuery . type ( function ( ) { } ) === "function"
77+ jQuery . type ( [ ] ) === "array"
78+ jQuery . type ( new Date ( ) ) === "date"
79+ jQuery . type ( new Error ( ) ) === "error" // as of jQuery 1.9
80+ jQuery . type ( / t e s t / ) === "regexp"
81+ // 其他一切都将返回类型“object”
82+
83+ // 总结:通常情况下用typeof判断,遇到预知Object类型的情况可以选用instanceof或constructor方法
84+
85+ </ script >
86+ </ body >
87+ </ html >
0 commit comments