当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript变量与类型检测总结

JavaScript变量与类型检测总结

2018年06月22日  | 移动技术网IT编程  | 我要评论

JavaScript中的变量类型:

基本类型Undefined,Null,Boolean,NumberString.

按值访问(可直接操作保存在变量中的变量值);

复制规则:当复制基本类型值时:两个变量完全独立,不会互相影响。如下所示:

var a = b = 1;
var c = a;
b = 2;
c = 3;
console.log(a); //1
console.log(b); //2
console.log(c); //3

引用类型值包含多个值的对象,是一种将数据(属性)与功能(方法)组织在一起的数据结构。

按引用访问(不能直接操作对象的内存空间,操作的实际上是对象的引用);

复制规则:当复制引用类型的值时,复制的其实是一个指针,该指针指向堆中的同一个对象;所以原变量和复制的变量实际上将引用同一个对象,即改变其中一个,另一个也会跟着变化。如下所示:

var cat = new Object();
var dog = cat;
dog.name = "tom";
console.log(cat.name); //tom

变量类型检测:

方法一:type of

有两种写法:typeof xxx ,typeof(xxx)

typeof 2 //   number
typeof null //   object
typeof undefined //  undefined
typeof '123' //    string
typeof true //     boolean

typeof {} //   object
typeof [] //   object
typeof (function(){}) //  function
typeof new Object() //object
typeof Object //function
typeof /[hbc]at/gi //object

可以看到,用type of 可以很好的区分出number,undefined,string,boolean这四种基本数据类型,但null会被判定为object.此外,该方法还能检测出函数和对象,但是并不能知道某个对象具体是什么类型的实例。

 

方法二:constructor

每一个对象实例都可以通过 constrcutor 对象访问它的构造函数:

var cat = new Cat();
console.log(cat.constructor === Cat) //  true
console.log(cat.constructor === Cat.prototype.constructor) //  true

故我们可以通过constructor对对象实例的类型进行判断,但是constructor 属性易变。我们有一种更好的判定方法:instanceof 操作符:更多constructor与原型有关的原理可参考博文:

 

方法三:instanceof

如果被检测变量是给定类型的实例,则返回true。

var strObj = new String(); 
strObj instanceof String //true
strObj  instanceof Object //true

如图所示,strObjString类型的实例,故返回true,另外,由于所有对象都是Object类型的实例,故第三行也返回true但其实该方法并没有这么简单,该方法是检测左侧的__proto__原型链上,是否存在右侧的prototype原型;即L.__proto__.__proto__ ..... === R.prototype 如果存在返回true 否则返回false关于instanceof和原型链的原理下面两篇文章说的比较清楚:

这种方法较为稳妥,却不能具体的检测出某个变量的具体类型。那有没有一种方法可以准确的检测出某个变量的类型呢?方法4便是:

 

方法四:Object.prototype.toString.call()

1.判断基本类型:

Object.prototype.toString.call(null);//”[object Null]”
Object.prototype.toString.call(undefined);//”[object Undefined]”
Object.prototype.toString.call("abdaw");//”[object String]”
Object.prototype.toString.call(1);//”[object Number]”
Object.prototype.toString.call(true);//”[object Boolean]”

2.判断原生引用类型:

//函数类型:
var foo = Function()
console.log(Object.prototype.toString.call(foo))//”[object Function]”
//日期类型:
var date = new Date();
console.log(Object.prototype.toString.call(date))//”[object Date]”
//数组类型:
var arr = [1,2,3];
console.log(Object.prototype.toString.call(arr))//”[object Array]”
//正则表达式:
var reg = /[hbc]at/gi;
console.log(Object.prototype.toString.call(reg))//”[object RegExp]”
//自定义类型:
function Cat(name) {
this.name = name;
}
var cat = new Cat("Tom");
console.log(Object.prototype.toString.call(cat)) //”[object Object]”

然而这种方法不能准确判断cat是Cat类的实例,这时可以结合instanceof 操作符来进行判断:person instanceof Person //true。

如有错误或补充,欢迎指出。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网