2

Is it possible to create a function in IE >= 9 with null as its prototype? (like in the code below, which unfortunately, works only in Chrome, FF, and Safari)

var F = function(){};
F.__proto__ = Object.create(null);
17
  • What do you mean? IEs don't support __proto__ Commented Nov 19, 2013 at 16:40
  • Are you using function F as a constructor function ? Commented Nov 19, 2013 at 16:42
  • 2
    Why not just set F.prototype = Object.create(null);. Commented Nov 19, 2013 at 16:54
  • 1
    Then use F = Object.create(null) Commented Nov 19, 2013 at 17:04
  • 1
    You could overwrite .apply and .call (and the other properties you don't want to be accessible) on the function object itself. But whatever you do, it's likely that it's still possible to use Function.prototype.apply.call(F, [arguments]), and after reading the other comments that might not be an issue in your case. Commented Nov 19, 2013 at 17:05

1 Answer 1

4

No, it's impossible without reassigning a [[prototype]] to a function object. IE does neither support the nonstandard __proto__ property nor the ES6 method Object.setPrototypeOf.

The only custom callable objects are functions which you create with function expressions/declarations or the Function constructor. However, all of these methods will create function objects which have their [[prototype]] set to the builtin Function.prototype object.


F will be used as a constructor (in a compiled to javascript language). But the code is going to be more complex. The core idea is that there will be one more line F.prototype = F after that, so all objects created from F will see the attributes of their class; i.e. f = new F(); F.foo = 123; print(f.foo);

Don't do that. While F is a (constructor) function, your instances are not, so there's no reason to let them inherit from it. Just comile your class attributes to F.prototype.foo = 123;. Or compile any accesses to static properties (where "instances see class attributes") to use the F object instead, so print(F.foo).

Sign up to request clarification or add additional context in comments.

7 Comments

"Don't do that. While F is a (constructor) function, your instances are not [snip]" Why? If you do F.prototype = F, then instances of new F will be regular JS objects. There is no performance penalty for that, but it gives you more flexible way of working with statics (and makes compiler simpler). What's the real downside?
As for the __proto__, yeah, I guess that's the only way. It also looks like Function.prototype is read-only, thus eliminating all possible hacks around it too.
On the bright side, __proto__ has landed in IE11: msdn.microsoft.com/en-us/library/ie/br212465(v=vs.94).aspx
Yes, they would be regular JS objects - not callable, so you don't want them to inherit from Function.prototype. However, F is a function and should inherit from Function.prototype. That means that the instances of F should not inherit from F like you had planned. If you could tell me more about (or link) your compiler, I can explain you how to keep static properties simple.
Thank you, Bergi. I'll send you a link once it's open-sourced ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.