Skip to content

Instantly share code, notes, and snippets.

@bhattisatish
Created December 26, 2012 05:56
Show Gist options
  • Save bhattisatish/4378255 to your computer and use it in GitHub Desktop.
Save bhattisatish/4378255 to your computer and use it in GitHub Desktop.
Is a function a constructor or not? Original src https://github.com/Benvie/Node.js-Ultra-REPL
function isConstructor(o){
return typeof o === 'function' // per ES spec any callable is typed 'function'
&& o.prototype != null // filter out absent prototypes
&& (typeof o.prototype === 'object' // filter out primitive prototypes
|| typeof o.prototype === 'function')
&& Object.getPrototypeOf(o) !== Object.prototype // filter out non-constructable DOM interfaces
&& Object.getOwnPropertyNames(o.prototype).length > // ensure the prototype has at least one property
{}.hasOwnProperty.call(o.prototype, 'constructor'); // ...besides 'constructor'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment