easierbycode
11/29/2012 - 9:33 PM

Get methods of an Object constructor or instance

Get methods of an Object constructor or instance

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"]