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

时间:2022-09-11 16:54:34

Hey I'm new to JavaScript, I love the MyClass.class and MyClass.methods in Ruby, are there any equivalence in JavaScript to check out the object type and methods available?

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

BTW the typeof operator seems to always return 'object', I don't know why.

BTW类型操作符似乎总是返回'对象',我不知道为什么。

2 个解决方案

#1


5  

Are there any equivalence in JavaScript to check out the object type..

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

The typeof operator does that, but it can be confusing with what it reports back.

typeof运算符可以做到这一点,但它可能会与报告的内容混淆。

For example, typeof null will tell you 'object', though it is not an object (though this behaviour is defined).

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

typeof 'a' will tell you 'string', but typeof new String('a') will tell you an 'object'.

typeof'a'会告诉你'string',但typeof new String('a')会告诉你'对象'。

The other advantage of typeof operator is it will not throw a ReferenceError if its operand has not yet been declared.

typeof运算符的另一个优点是,如果尚未声明其操作数,它将不会抛出ReferenceError。

The methods used below to determine a function can be adapted to report the correct type (though typeof is generally enough for primitives).

下面用于确定函数的方法可以适用于报告正确的类型(尽管typeof通常足以用于基元)。

...and methods available?

......和方法可用吗?

You can view all the properties on an object with a for ( in ) loop.

您可以使用for(in)循环查看对象上的所有属性。

for (var prop in obj) {
   console.log(prop);
}

This will show all enumerable properties, including ones inherited/delegated. To disregard inherited properties, add this to the body of the loop...

这将显示所有可枚举的属性,包括继承/委托的属性。要忽略继承的属性,请将其添加到循环体中...

if ( ! obj.hasOwnProperty(prop)) {
   continue;
}

To view methods (properties assigned a function), you can do this...

要查看方法(分配了函数的属性),您可以执行此操作...

for (var prop in obj) {
    if (!obj.hasOwnProperty(prop) || Object.prototype.toString.call(obj[prop]) != '[object Function]') {
        continue;
    }
   console.log(prop, obj[prop]);
}

jsFiddle.

If not in a multi window environment (i.e. not iframes), you can simply use...

如果不是在多窗口环境中(即不是iframe),你可以简单地使用......

for (var prop in obj) {
    if (!obj.hasOwnProperty(prop) || ! (obj[prop] instanceof Function)) {
        continue;
    }
   console.log(prop, obj[prop]);
}

jsFiddle.

...or...

for (var prop in obj) {
    if (!obj.hasOwnProperty(prop) || obj[prop].constructor != Function) {
        continue;
    }
   console.log(prop, obj[prop]);
}

jsFiddle.

If you only care about methods that implement [[Call]] (i.e. can be invoked as a function), such as the RegExp objects in older Safaris, you can simply determine what is invokable with typeof fn == 'function'.

如果您只关心实现[[Call]]的方法(即可以作为函数调用),例如较旧的Safaris中的RegExp对象,则可以使用typeof fn =='function'简单地确定可调用的内容。

Since you mentioned Ruby, you could be completely crazy and implement Ruby's class (or close enough) and methods by augmenting Object.prototype, but please don't. :)

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

I also have an in-depth article on the typeof operator in JavaScript.

我还有一篇关于JavaScript中typeof运算符的深入文章。

#2


0  

In JavaScript everything is an object and Functions are first class JavaScript Objects.

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

Use this snippet if you wanna know the type of all objects.

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

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();
            }
        }
    }
};

Now call it like this.

现在这样称呼它。

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

jsfiddle here

#1


5  

Are there any equivalence in JavaScript to check out the object type..

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

The typeof operator does that, but it can be confusing with what it reports back.

typeof运算符可以做到这一点,但它可能会与报告的内容混淆。

For example, typeof null will tell you 'object', though it is not an object (though this behaviour is defined).

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

typeof 'a' will tell you 'string', but typeof new String('a') will tell you an 'object'.

typeof'a'会告诉你'string',但typeof new String('a')会告诉你'对象'。

The other advantage of typeof operator is it will not throw a ReferenceError if its operand has not yet been declared.

typeof运算符的另一个优点是,如果尚未声明其操作数,它将不会抛出ReferenceError。

The methods used below to determine a function can be adapted to report the correct type (though typeof is generally enough for primitives).

下面用于确定函数的方法可以适用于报告正确的类型(尽管typeof通常足以用于基元)。

...and methods available?

......和方法可用吗?

You can view all the properties on an object with a for ( in ) loop.

您可以使用for(in)循环查看对象上的所有属性。

for (var prop in obj) {
   console.log(prop);
}

This will show all enumerable properties, including ones inherited/delegated. To disregard inherited properties, add this to the body of the loop...

这将显示所有可枚举的属性,包括继承/委托的属性。要忽略继承的属性,请将其添加到循环体中...

if ( ! obj.hasOwnProperty(prop)) {
   continue;
}

To view methods (properties assigned a function), you can do this...

要查看方法(分配了函数的属性),您可以执行此操作...

for (var prop in obj) {
    if (!obj.hasOwnProperty(prop) || Object.prototype.toString.call(obj[prop]) != '[object Function]') {
        continue;
    }
   console.log(prop, obj[prop]);
}

jsFiddle.

If not in a multi window environment (i.e. not iframes), you can simply use...

如果不是在多窗口环境中(即不是iframe),你可以简单地使用......

for (var prop in obj) {
    if (!obj.hasOwnProperty(prop) || ! (obj[prop] instanceof Function)) {
        continue;
    }
   console.log(prop, obj[prop]);
}

jsFiddle.

...or...

for (var prop in obj) {
    if (!obj.hasOwnProperty(prop) || obj[prop].constructor != Function) {
        continue;
    }
   console.log(prop, obj[prop]);
}

jsFiddle.

If you only care about methods that implement [[Call]] (i.e. can be invoked as a function), such as the RegExp objects in older Safaris, you can simply determine what is invokable with typeof fn == 'function'.

如果您只关心实现[[Call]]的方法(即可以作为函数调用),例如较旧的Safaris中的RegExp对象,则可以使用typeof fn =='function'简单地确定可调用的内容。

Since you mentioned Ruby, you could be completely crazy and implement Ruby's class (or close enough) and methods by augmenting Object.prototype, but please don't. :)

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

I also have an in-depth article on the typeof operator in JavaScript.

我还有一篇关于JavaScript中typeof运算符的深入文章。

#2


0  

In JavaScript everything is an object and Functions are first class JavaScript Objects.

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

Use this snippet if you wanna know the type of all objects.

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

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();
            }
        }
    }
};

Now call it like this.

现在这样称呼它。

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

jsfiddle here