欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript中typeof操作符和constucor屬性檢測

 更新時間:2015年02月26日 14:33:50   投稿:hebedich  
這篇文章主要介紹了javascript中typeof操作符和constucor屬性檢測的相關(guān)資料,需要的朋友可以參考下

*#type.js

復(fù)制代碼 代碼如下:

function Person(name, age) {
 this.name = name;
 this.age = age;
}
var d = {an: 'object'};
var a = ['apple', 'banana'];
var f = function() {};
var s = 'David';
var n = 33;
var b = true;
var o = new Object();
var person = new Person('Mark', 22);
console.log(typeof(d) + ': ' + d.constructor);
console.log(typeof(a) + ': ' + a.constructor);
console.log(typeof(f) + ': ' + f.constructor);
console.log(typeof(s) + ': ' + s.constructor);
console.log(typeof(n) + ': ' + n.constructor);
console.log(typeof(b) + ': ' + b.constructor);
console.log(typeof(o) + ': ' + o.constructor);
console.log(typeof(person) + ': ' + person.constructor);

運行$node type.js得

復(fù)制代碼 代碼如下:

object:   function Object() { [native code] }
object:   function Array() { [native code] }
function: function Function() { [native code] }
string:   function String() { [native code] }
number:   function Number() { [native code] }
boolean:  function Boolean() { [native code] }
object:   function Object() { [native code] }
object:   function Person() { [native code] }

可見, 使用typeof操作符和constucor屬性檢測對象類型返回值是存在差異的.

如果變量是數(shù)組, typeof操作符返回object, constructor屬性返回Array;
如果變量是構(gòu)造函數(shù)對象, typeof操作符返回object, constructor屬性返回該構(gòu)造函數(shù)
每個變量都有其construcor屬性, 這個屬性不單單提供了這個是否對象, 還提供了這個對象是什么類型的對象. 總之, constructor屬性保存了一個指向?qū)ο蟮臉?gòu)造函數(shù), 無論它是自定義的還是原生類型的對象.

有一點需要注意的是, 不同的瀏覽器對typeof操作符檢測正則表達式會有所不同,IE和Firefox會返回'object'.

好了,今天內(nèi)容就先到這里了,小伙伴們?nèi)缬幸蓡?,就在下方留言吧?/p>

相關(guān)文章

最新評論