Are there any equivalence in JavaScript to check out the object type..
The typeof operator does that, but it can be confusing with what it reports back.
For example, typeof null will tell you 'object', though it is not an object (though this behaviour is defined).
typeof 'a' will tell you 'string', but typeof new String('a') will tell you an 'object'.
The other advantage of typeof operator is it will not throw a ReferenceError if its operand has not yet been declared.
The methods used below to determine a function can be adapted to report the correct type (though typeof is generally enough for primitives).
...and methods available?
You can view all the properties on an object with a for ( in ) loop.
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...
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'.
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. :)
I also have an in-depth article on the typeof operator in JavaScript.