嘿,我是JavaScript新手,我喜歡Ruby中的MyClass.class
和MyClass.methods
,JavaScript中是否有任何等價的方法來檢查可用的對象類型和方法?如何在Javascript中檢查該對象可用的對象和方法
BTW的typeof
運營商似乎總是返回'object'
,我不知道爲什麼。
嘿,我是JavaScript新手,我喜歡Ruby中的MyClass.class
和MyClass.methods
,JavaScript中是否有任何等價的方法來檢查可用的對象類型和方法?如何在Javascript中檢查該對象可用的對象和方法
BTW的typeof
運營商似乎總是返回'object'
,我不知道爲什麼。
是否有任何的JavaScript等價檢查出的對象類型..
的typeof
運營商這樣做,但它可以與它報告回混亂。
例如,typeof null
會告訴你'object'
,儘管它不是一個對象(雖然這是行爲的定義)。
typeof 'a'
會告訴你'string'
,但typeof new String('a')
會告訴你一個'object'
。
typeof
運算符的另一個優點是,如果其操作數尚未聲明,它不會拋出ReferenceError
。
下面用於確定函數的方法可以適於報告正確的類型(儘管通常typeof
是足夠的原語)。
...和方法可用?
您可以使用for (in)
循環查看對象上的所有屬性。
for (var prop in obj) {
console.log(prop);
}
這將顯示所有可枚舉的屬性,包括繼承/委託的屬性。無視繼承屬性,將其添加到循環體...
if (! obj.hasOwnProperty(prop)) {
continue;
}
要查看方法(屬性分配功能),你可以這樣做......
for (var prop in obj) {
if (!obj.hasOwnProperty(prop) || Object.prototype.toString.call(obj[prop]) != '[object Function]') {
continue;
}
console.log(prop, obj[prop]);
}
jsFiddle 。
如果不是在多window
環境(即不iframe
S),你可以簡單地使用...
for (var prop in obj) {
if (!obj.hasOwnProperty(prop) || ! (obj[prop] instanceof Function)) {
continue;
}
console.log(prop, obj[prop]);
}
......或者......
for (var prop in obj) {
if (!obj.hasOwnProperty(prop) || obj[prop].constructor != Function) {
continue;
}
console.log(prop, obj[prop]);
}
如果您只關心實施[[Call]]
(即可以作爲函數調用)的方法,例如RegExp
objects in older Safaris,那麼您可以簡單地確定typeof fn == 'function'
可調用的內容。
既然您提到過Ruby,那麼您可能會完全瘋狂並通過增加Object.prototype
來實現Ruby的class
(或足夠接近)和methods
,但請不要。 :)
我也有一篇關於typeof
operator in JavaScript的深度文章。
在JavaScript中,一切都是對象,函數是第一類JavaScript對象。
如果您想知道所有對象的type
,請使用此片段。
var is = {
Null: function (a) {
return a === null;
},
Undefined: function (a) {
return a === undefined;
},
nt: function (a) {
return (a === null || a === undefined);
},
Function: function (a) {
return (typeof (a) === 'function') ? a.constructor.toString().match(/Function/) !== null : false;
},
String: function (a) {
return (typeof (a) === 'string') ? true : (typeof (a) === 'object') ? a.constructor.toString().match(/string/i) !== null : false;
},
Array: function (a) {
return (typeof (a) === 'object') ? a.constructor.toString().match(/array/i) !== null || a.length !== undefined : false;
},
Boolean: function (a) {
return (typeof (a) === 'boolean') ? true : (typeof (a) === 'object') ? a.constructor.toString().match(/boolean/i) !== null : false;
},
Date: function (a) {
return (typeof (a) === 'date') ? true : (typeof (a) === 'object') ? a.constructor.toString().match(/date/i) !== null : false;
},
HTML: function (a) {
return (typeof (a) === 'object') ? a.constructor.toString().match(/html/i) !== null : false;
},
Number: function (a) {
return (typeof (a) === 'number') ? true : (typeof (a) === 'object') ? a.constructor.toString().match(/Number/) !== null : false;
},
Object: function (a) {
return (typeof (a) === 'object') ? a.constructor.toString().match(/object/i) !== null : false;
},
RegExp: function (a) {
return (typeof (a) === 'function') ? a.constructor.toString().match(/regexp/i) !== null : false;
}
};
var type = {
of: function (a) {
for (var i in is) {
if (is[i](a)) {
return i.toLowerCase();
}
}
}
};
現在就這樣稱呼它。
var a= [];
var b ={};
var c = document.getElementById("c");
var d = function(){};
var e = "";
var f = 5;
alert(type.of(a)); //alerts array
alert(type.of(b)); //alerts object
alert(type.of(c)); //alerts html
alert(type.of(d)); //alerts function
alert(type.of(e)); //alerts string
alert(type.of(f)); //alerts number
的'undefined'檢查應該比較'無效()'。這也不能在多窗口環境下工作。另外,Firefox說一個正則表達式是一個「對象」。 WebKit表示正則表達式是「函數」。 – alex 2011-06-10 05:04:36
只是顯示瞭如何輕鬆檢索type.of。 :)我知道代碼有它的錯誤 – naveen 2011-06-10 05:23:26
公平不夠,但如果代碼有錯誤你知道,爲什麼你不修復它們? :) – alex 2011-06-10 05:25:21