chenming142's Blog for github

实践|思考|总结|分享

jQuery 数据类型详解

ECMAScript中有5种基本数据类型:Undefined,Null,Boolean,Number和String.还有一种复杂数据类型——Object,Object本质上是由一组无序的名值对组成的.

typeof 操作符

对一个值使用typeof操作符可能返回下列某个字符串:
“undefined” —— 如果这个值未定义
“boolean” —— 如果这个值是布尔值
“string” —— 如果这个值是字符串
“number” —— 如果这个值是数值
“object” —— 如果这个值是对象或null
“function” —— 如果这个值是函数

注:typeof 操作符的操作数可以是变量,也可以是数值字面量.typeof是操作符而非函数.

使用库进行判断数据类型

Javascript Array Syntax MDN Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
   (function($, S, undefined){
      var host = this,
          class2type = {}, // [[Class]] -> type pairs
          OP = Object.prototype,
          toString = OP.toString,
          hasOwn = OP.hasOwnProperty,
          TRUE = true,FALSE = false;
      $ = host && ( host[$] = {} ) ;

      var hasEnumBug = !({toString:1}.propertyIsEnumerable('toString')),
          emumProperties = [
              'hasOwnProperty',
              'isPrototypeOf',
              'propertyIsEnumerable',
              'toString',
              'toLocaleString',
              'valueOf'
          ],
          meta = {
              /**
              * Copies all the properties of s to r
              * @param r
              * @param s
              * @param ov
              * @param wl
              * @param deep
              * @return {*}
              */
              mix : function(r, s, ov, wl, deep){
                  if(!s || !r){return r;}
                  if(ov === undefined){ov = true;}
                  var i, p, len;
                  if(wl && (len = wl.length)){
                      for(i = 0; i < len; i++){
                          p = wl[i];
                          if( p in s){_mix(p, r, s, ov, deep);}
                      }
                  }else{
                      for(p in s){
                          _mix(p, r, s, ov, deep);
                      }
                      if(hasEnumBug){
                          for (var j = 0; j < emumProperties.length; j++) {
                              p = emumProperties[j];
                              if (ov && hasOwn.call(s, p)) {
                                  r[p] = s[p];
                              }
                          }
                      }
                  }
                  return r;
              }
          },
          _mix = function(p, r, s, ov, deep){
              if(ov || !(p in r)){
                  var target = r[p], src = s[p];
                  if(target === src){return ;}
                  if( deep && src && (S.isArray(src)) ){
                      var clone = target && (S.isArray(target) || S.isPlainObject(target)) ?
                          target :
                          ( S.isArray(src) ? [] : {});
                      r[p] = S.mix( clone, src, ov, undefined);
                  }else if( src !== undefined ){
                      r[p] =s[p];
                  }
              }
          },
          seed = ( host && host[S] ) || {};
      S = host[S] = meta.mix( seed, meta );

      /**
      * If the type of val is null, undefined, number, string, boolean, return true.
      * @param val
      * @return {Boolean}
      */
      function isValidParamValue( val ){
          var t = typeof val;
          return val == null || ( t !== 'object' && t !== 'function' );
      }
      S.mix(KISSY, {
          isNull : function( o ){
              return o === null;
          },
          isUndefined : function( o ){
              return o === undefined;
          },
          isEmptyObject : function(obj){
              for(var name in obj){
                  if( name !== undefined ){
                      return FALSE;
                  }
              }
              return TRUE;
          },
          isPlainObject : function(o){
              return o && toString.call(o) === '[object Object]' && 'isPrototypeOf' in o;
          },
          type : function( o ){
              return o == null ?
                  String( o ) : class2type[ toString.call(o) ] || "object";
          },
          each : function(object, fn, context){
              if( object ){
                  var key, val, i = 0,
                      length = object && object.length,
                      isObj = length === undefined || S.type(object) === 'function';
                  context = context || null;
                  if( isObj ){
                      for( key in object ){
                          if( fn.call( context, object[key], key, object ) === FALSE ){
                              break;
                          }
                      }
                  }else{
                      for (val = object[0];
                           i < length && fn.call(context, val, i, object) !== FALSE; val = object[++i]) {
                      }
                  }
              }
              return object;
          }
      });
      S.each('Boolean Number String Function Array Date RegExp Object'.split(' '),function(name, lc){
          class2type['[object ' + name + ']'] = (lc = name.toLowerCase());
          S['is' + name] = function( o ){
              return S.type(o) == lc;
          }
      });

      $.extend = function( ){
          var options, name, src, copy, copyIsArray, clone,
              target = arguments[0] || {},
              i = 1,
              length = arguments.length,
              deep = false;
          if( typeof target === "boolean" ){
              deep = target;
              target = arguments[1] || {};
              i = 2;
          }
          if( typeof target !== "object" && !$.isFunction(target)){
              target = {};
          }
          if( length === i ){//extend jQuery itself
              target = this;
              --i;
          }
          for( ; i < length; i++){
              if( (options = arguments[i]) != null ){
                  for( name in options ){
                      src = target[ name ];
                      copy = options[ name ];
                      if( target === copy ){continue;}
                      if( deep && copy && ( $.isPlainObject(copy) || ( copyIsArray = $.isArray(copy) ) ) ){
                          if( copyIsArray ){
                              copyIsArray = false;
                              clone = src && $.isArray(src) ? src : [];
                          }else{
                              clone = src && $.isPlainObject(src) ? src : {};
                          }
                          target[ name ] = $.extend( deep, clone, copy );
                      }else if( copy !== undefined ){
                          target[ name ] = copy;
                      }
                  }
              }
          }
          return target;
      };
      $.extend({
          isFunction :function(obj){
              return $.type(obj) === 'function';
          },
          isArray : Array.isArray || function(obj){
              return $.type(obj) === 'array';
          },
          isNumeric : function(obj){
              return !isNaN( parseFloat(obj) ) && isFinite(obj);
          },
          isWindow : function(obj){
              return obj != null && obj == obj.window;
          },
          type : function( o ){
              return o == null ?
                  String( o ) : class2type[ toString.call(o) ] || "object";
          },
          isEmptyObject : function(obj){
              for(var name in obj){
                  if( name !== undefined ){
                      return FALSE;
                  }
              }
              return TRUE;
          },
          isPlainObject : function(obj){
              if( !obj || $.type(obj) !== "object" || obj.nodeType || $.isWindow(obj) ){
                  return false;
              }
              try{
                  if( obj.constructor &&
                      !hasOwn.call(obj, "constructor") &&
                      !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")){
                      return false;
                  }
              } catch (e){
                  return false;
              }
              var key;
              for( key in obj ){}
              return key === undefined || hasOwn.call( obj, key );
          },
          each : function( object, callback, args ){
              var name, i = 0,
                  length = object.length,
                  isObj = length === undefined || $.isFunction( object );
              if( args ){
                  if( isObj ){
                      for( name in object ){
                          if( callback.apply( object[name] , args) === false ){break;}
                      }
                  }else{
                      for( ; i < length; ){
                          if( callback.apply( object[i++], args) === false){break;}
                      }
                  }
              }else{
                  if ( isObj ) {
                      for ( name in object ) {
                          if ( callback.call( object[ name ], name, object[ name ] ) === false ) {break;}
                      }
                  } else {
                      for ( ; i < length; ) {
                          if ( callback.call( object[ i ], i, object[ i++ ] ) === false ) {break;}
                      }
                  }
              }
              return object;
          }
      });
      $.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name){
          class2type[ "[object "+ name +"]" ] = name.toLowerCase();
      });
  })('jQuery', 'KISSY', undefined);

发表评论