开发者

how to check the object and methods available for this object in Javascript

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?

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


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.


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜