RE: Faking host objects

> I couldn't resist… I plugged in bits of your code and it worked :D You rock, François! 
> 
> Though, I'm still confused as to where I screwed up. Maybe you can help me understand? 
I did walk in the ECMAScript 5 spec a bit to solve the remaining issues.
[#] Firstly, I wanted to fix you 'fooInstance instanceof Foo' problem.
 - 'o instanceof f' uses 'f.[[HasInstance]](o)' [http://ecma-international.org/ecma-262/5.1/#sec-11.8.6]
 - The default [[HasInstance]] algorithm verrify that 'f.prototype' is in the prototype chain of 'o' [http://ecma-international.org/ecma-262/5.1/#sec-15.3.5.3]
That means that 'publicConstructor.prototype' has to be the same object has 'privateConstructor.prototype'. If you do that, you solve the problem of 'test2' I outlined as well.
[#] However, this technique is leaking the private constructor of 'fooInstance' via 'fooInstance.constructor', inherited from privateConstructor.prototype.constructor, so I modified the constructor property to return the publicConstructor instead. [http://ecma-international.org/ecma-262/5.1/#sec-4.3.4]
[#] Finally, you have to teak the 'toString' method of the prototype to return "[object Foo]" on an instance, but "[object FooPrototype]" on the prototype. To to that, I use a special constructor and I modify his prototype a bit; this introduce an intermediary inheritance level, but this is difficult to see from the user side (ultimately, you'll have to leak something).
[#] Last but not least, the public constructor should work. To do so, I use the '[[Construct]]' algorithm which returns the return value of the function instead of 'this' if it's an object. [http://ecma-international.org/ecma-262/5.1/#sec-13.2.2]

> While yours has enumerable true. No biggy. 

Good catch. My code is just a draft, I'm gonna perfect it today.

 
> I had problems with eval() and strict mode, which is why I used the function constructor. 
Function(...) is nice in every browser, except FireFox where a bug prevents the functions created using the Function constructor from being optimized; this is why eval is better. 		 	   		  

Received on Sunday, 30 December 2012 20:02:38 UTC