码迷,mamicode.com
首页 > 其他好文 > 详细

instanceof

时间:2015-03-30 15:55:26      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

 

 

// primitive values
var myNull = null;
var myUndefined = undefined;
var primitiveString1 = "string";
var primitiveString2 = String(‘string‘);
var primitiveNumber1 = 10;
var primitiveNumber2 = Number(‘10‘);
var primitiveBoolean1 = true;
var primitiveBoolean2 = Boolean(‘true‘);

console.log(typeof myNull); // logs object? WHAT? Be aware...
console.log(typeof myUndefined); // logs undefined
console.log(typeof primitiveString1, typeof primitiveString2); // logs string string
console.log(typeof primitiveNumber1, typeof primitiveNumber2); // logs number number
console.log(typeof primitiveBoolean1, typeof primitiveBoolean2); // logs boolean boolean

// Complex Values
var myNumber = new Number(23);
var myString = new String(‘male‘);
var myBoolean = new Boolean(false);
var myObject = new Object();
var myArray = new Array(‘foo‘, ‘bar‘);
var myFunction = new Function("x", "y", "return x * y");
var myDate = new Date();
var myRegExp = new RegExp(‘\\bt[a-z]+\\b‘);
var myError = new Error(‘Crap!‘);

console.log(typeof myNumber); // logs object
console.log(typeof myString); // logs object
console.log(typeof myBoolean); // logs object
console.log(typeof myObject); // logs object
console.log(typeof myArray); // logs object
console.log(typeof myFunction); // logs function? WHAT? Be aware...
console.log(typeof myDate); // logs object
console.log(typeof myRegExp); // logs object? WHAT? Be aware...

console.log(typeof myError); // logs object


// instanceof 只适用于构造函数创建返回的复杂对象和实例

// user-defined object constructor
var CustomConstructor = function() {this.foo = ‘bar‘;};

//instantiate an instance of CustomConstructor
var instanceOfCustomObject = new CustomConstructor(); 

console.log(instanceOfCustomObject instanceof CustomConstructor); // logs true

// works the same as a native object
console.log(new Array(‘foo‘) instanceof Array) // logs true

  

instanceof

标签:

原文地址:http://www.cnblogs.com/daqianduan/p/4378200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!