如何在Javascript中检查此对象可用的对象和方法

如何在Javascript中检查此对象可用的对象和方法,javascript,Javascript,嘿,我是JavaScript新手,我喜欢Ruby中的MyClass.class和MyClass.methods,JavaScript中是否有等价物来检查可用的对象类型和方法 顺便说一句,typeof操作符似乎总是返回'object',我不知道为什么 JavaScript中是否存在任何等价物来检查对象类型 typeof操作符可以做到这一点,但它可能会与它报告的内容相混淆 例如,typeof null将告诉您“对象”,尽管它不是对象(尽管定义了此行为) typeof'a'将告诉您'string',但

嘿,我是JavaScript新手,我喜欢Ruby中的
MyClass.class
MyClass.methods
,JavaScript中是否有等价物来检查可用的对象类型和方法

顺便说一句,
typeof
操作符似乎总是返回
'object'
,我不知道为什么

JavaScript中是否存在任何等价物来检查对象类型

typeof
操作符可以做到这一点,但它可能会与它报告的内容相混淆

例如,
typeof null
将告诉您
“对象”
,尽管它不是对象(尽管定义了此行为)

typeof'a'
将告诉您
'string'
,但
typeof'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]);
}

如果不在多
窗口
环境中(即不在
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]]
(即可以作为函数调用)的方法,例如,您可以简单地确定使用
typeof fn==“function”
可以调用什么

既然你提到了Ruby,你可能会完全发疯,通过增加Object.prototype来实现Ruby的
类(或者足够接近)和
方法,但请不要:)


我还有一篇深入的文章,介绍了JavaScript中的。

一切都是对象,函数都是一流的JavaScript对象

如果您想知道所有对象的
类型,请使用此代码段

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

未定义的
检查应与
void()
进行比较。这在多窗口环境中也不起作用。另外,Firefox说正则表达式是一个
对象
。WebKit说正则表达式是
函数
。刚才向op演示了如何轻松检索类型。:)我知道代码有很多错误,但是如果代码有你知道的错误,为什么不修复它们呢?:)@亚历克斯:因为我不知道确切的虫子。我早些时候从谷歌搜索中得到了这个,并将其存储起来以备将来参考。我知道未定义在我的测试中不起作用。我没有检查正则表达式,因为我认为这是我的复仇女神。我把从每天开始学习regex放到第二天:)我测试了其余的,发现OP可能会用到它。就这些。如果有时间,请随意编辑。