js中较常用的基本数据类型有:

值种类(基本上种类):字符串数组,数据,布尔值,对空(空),未定义,标记。

参照基本数据类型:目标,二维数组解析函数。

举好多个以上基本数据类型的事例:

// 字符串数组(String)let a='word';// 数据(Number)let b=123;// 布尔运算(Boolean)let c=true;// 对空(Null)let d=null;// 未定义(Undefined)let e;// Symbollet f=Symbol('123');// 目标(Object)let g={name:'xiangming'};// 二维数组(Array)let h=[1,2,3,4,5];// 涵数(Function)let i=function () { return 'done';};

1.最普遍的分辨方式:typeof。

console.log('a:' typeof a); // a:stringconsole.log('b:' typeof b); // b:numberconsole.log('c:' typeof c); // c:booleanconsole.log('d:' typeof d); // d:objectconsole.log('e:' typeof e); // e:undefinedconsole.log('f:' typeof f); // f:symbolconsole.log('g:' typeof g); // g:objectconsole.log('h:' typeof h); // h:objectconsole.log('i:' typeof i); // i:function

因而,在分辨目标种类之间的另一半时,typeof更便捷。

2.分辨已经知道物件种类的方式:instanceof。

console.log('a is string:' a instanceof String);// a is string:trueconsole.log('b is number:' b instanceof Number);// b is number:trueconsole.log('b is number:' b instanceof number);// Uncaught ReferenceError: number is not defined

留意:instanceof后边一定跟一个目标种类,英文大小写不可以不正确。这类方式适用一些标准挑选或支系。

3.普遍但繁杂的方式:原形(强烈推荐)。

// 定义方法获得变量类型function var_type(data) { return Object.prototype.toString.call(data).replace(/^\[object\s(. )\]$/, '$1').toLowerCase();}console.log('a:' var_type(a));// a:stringconsole.log('b:' var_type(b));// b:numberconsole.log('c:' var_type(c));// c:booleanconsole.log('d:' var_type(d));// d:nullconsole.log('e:' var_type(e));// e:undefinedconsole.log('f:' var_type(f));// f:symbolconsole.log('g:' var_type(g));// g:objectconsole.log('h:' var_type(h));// h:arrayconsole.log('i:' var_type(i));// i:function

尽管这一方式有点儿不便,可是只要简易的装包一个方式。

最终,推存一个小编自身写的云景管理方法的通用性环境架构。小编也是第一次尝试开放源码,期待我们能帮我提议。

评论(0条)

刀客源码 游客评论