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

Hi, I am new to JavaScript, I like MyClass.class, and MyClass.methodsin the Ruby, is there any equivalent in JavaScript, to check the type and methods of an object?

By the way, the operator typeofseems to always return 'object', I don’t know why.

+3
source share
2 answers

Is there any equivalence in JavaScript to check the type of an object.

The operator typeofdoes this, but may be confused by what he reports.

For example, it typeof nullwill tell you 'object'although this is not an object (although this behavior is defined).

typeof 'a'will let you know 'string', but typeof new String('a')let you know '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.

multi window (.. iframe s), ...

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

jsFiddle.

... ...

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

jsFiddle.

, [[Call]] (.. ), RegExp Safaris, , invokable typeof fn == 'function'.

Ruby, Ruby class ( ) methods, Object.prototype, , , .:)

typeof JavaScript.

+5

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

jsfiddle

0

All Articles