Created
March 13, 2013 05:20
-
-
Save junosuarez/5149561 to your computer and use it in GitHub Desktop.
What's the best way to build a function with prototypically inherited properties which still evaluates to typeof => 'function'?
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 proto = {foo: true} | |
var f1 = Object.create(proto, function () { return 1 }) | |
f1() // => 1 | |
f1.foo // => true | |
// :) | |
typeof f1 // => 'object' | |
// :( | |
var f2 = function () { return 2 }) | |
f2.__proto__ = proto // :( | |
f2() // => 2 | |
f2.foo // => true | |
typeof f2 // => 'function' | |
// :) | |
// is there a way without using __proto__? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hmm. The problem with f2 is that it's now not
instanceof Function
, which also means it doesn't have the methods inFunction.prototype
. When I tryproto = Object.create(Function.prototype, {})
strange things happen. Unfortunately I don't have time to fully understand what I'm seeing now, and I get the intuition that I'm "doing it wrong".