Created
November 29, 2012 21:33
-
-
Save easierbycode/4172057 to your computer and use it in GitHub Desktop.
Get methods of an Object constructor or instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Klass = function () { | |
this.someProp = 'someVal'; | |
}; | |
Klass.classMethod = function (arg) { | |
return arg; | |
}; | |
Klass.prototype.instanceMethod = function (arg) { | |
return arg; | |
}; | |
Klass.someNamespace = { | |
someMethod: function() {} | |
}; | |
// 'class' methods and properties | |
Object.getOwnPropertyNames( Klass ); | |
//=> ["length", "name", "arguments", "caller", "prototype", "classMethod", "someNamespace"] | |
// Instance methods | |
var myKlass = new Klass; | |
_.keys( myKlass.__proto__ ); | |
//=> ["instanceMethod"] | |
// Instance properties | |
Object.getOwnPropertyNames( myKlass ); | |
//=> ["someProp"] | |
// Methods and properties available within a namespace | |
Object.getOwnPropertyNames( Klass.someNamespace ); | |
//=> ["someMethod"] | |
// Methods available on all objects | |
Object.getOwnPropertyNames( Object.prototype ); | |
//=> ["constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable", "__defineGetter__", "__lookupGetter__", "__defineSetter__", "__lookupSetter__"] | |
// Methods available on all functions | |
Object.getOwnPropertyNames( Function.prototype ); | |
//=> ["constructor", "toSource", "toString", "apply", "call", "bind", "isGenerator", "length", "name", "arguments", "caller"] | |
// Example using Google Maps - | |
// Instance methods | |
var map = new google.maps.Map; | |
_.keys( map.__proto__ ); | |
//=> ["constructor", "streetView_changed", "getDiv", "M", "panBy", "panTo", "panToBounds", "fitBounds", "getBounds", "getStreetView", "setStreetView", "getCenter", "setCenter", "getZoom", "setZoom", "getMapTypeId", "setMapTypeId", "getProjection", "getHeading", "setHeading", "getTilt", "setTilt"] | |
// Methods and properties available within a namespace | |
Object.getOwnPropertyNames( google.maps.event ); | |
//=> ["ke", "od", "addListener", "cf", "removeListener", "clearListeners", "clearInstanceListeners", "trigger", "addDomListener", "addDomListenerOnce", "T", "bind", "addListenerOnce", "forward", "Ga", "Mh", "Rj"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment