From: Asen Bozhilov on 15 Jun 2010 04:03 Lasse Reichstein Nielsen wrote: > Dmitry A. Soshnikov writes: > > Yes, it's possible: <URL:http://bit.ly/aboVXU> -- implemented by > > Bozhilov; I've updated the article. > > It's distinguishable from the correct behavior. If you use the bound > function as a method of an instance created from it, then it will > believe it to be a construct call, even if it isn't. Yes that is true, but is it too far? For example with built in `Object.create' and `bind' you cannot create an instance by bound function. Built-in `bind' return function object who does not have `prototype' property. So in normal case `Object.create' should throw TypeError. You should need explicitly define `prototype' property and after that create an instance. > The problem is that you can't identify a construct call > programmatically. Testing whether "this" is an instance of the > function being created can be faked. Indeed, but as I said emulation of something does not need to implement all features of original. That approach works in certain cases and probably I should describe the possible problems by usage of it.
From: Dmitry A. Soshnikov on 15 Jun 2010 04:31 On 15.06.2010 12:03, Asen Bozhilov wrote: > Lasse Reichstein Nielsen wrote: >> Dmitry A. Soshnikov writes: > >>> Yes, it's possible:<URL:http://bit.ly/aboVXU> -- implemented by >>> Bozhilov; I've updated the article. >> >> It's distinguishable from the correct behavior. If you use the bound >> function as a method of an instance created from it, then it will >> believe it to be a construct call, even if it isn't. > Yes, there are also some other distinguishes, e.g.: print(new F1(3) instanceof F1); // false Or e.g. Asen's implementation dependent on `prototype' property of the bound function, which shouldn't have such property. > Yes that is true, but is it too far? For example with built in > `Object.create' and `bind' you cannot create an instance by bound > function. Built-in `bind' return function object who does not have > `prototype' property. So in normal case `Object.create' should throw > TypeError. You should need explicitly define `prototype' property and > after that create an instance. > What do you mean? Object.create accepts any object as a future prototype of the being created object. It doesn't matter, whether you define `prototype' property on bound function and pass `BoundF.prototype' to the Object.create, or just a single object. Anyway, defining `BoundF.prototype' will not change anything in respect of using `new' operator, because prototype of the newly created object is taken from the TargetF.prototype. >> The problem is that you can't identify a construct call >> programmatically. Testing whether "this" is an instance of the >> function being created can be faked. > > Indeed, but as I said emulation of something does not need to > implement all features of original. That approach works in certain > cases and probably I should describe the possible problems by usage of > it. > Yes, true, as I mentioned, emulation -- is emulation. For example, how other emulate `Object.freeze'? there is no way to do this in ES3, so they just return the object itself -- just the `Object.freeze' is present now as it would do something. But taking into account that some distinguishes I nevertheless update the article again, restoring the state that "it's not possible to implement spec's behavior for the "bind", although an acceptable emulation can be made, e.g. -- <and the link to your code here>". Dmitry.
From: Asen Bozhilov on 15 Jun 2010 04:48 Dmitry A. Soshnikov wrote: > Asen Bozhilov wrote: > > Yes that is true, but is it too far? For example with built in > > `Object.create' and `bind' you cannot create an instance by bound > > function. Built-in `bind' return function object who does not have > > `prototype' property. So in normal case `Object.create' should throw > > TypeError. You should need explicitly define `prototype' property and > > after that create an instance. > > What do you mean? Object.create accepts any object as a future prototype > of the being created object. It doesn't matter, whether you define > `prototype' property on bound function and pass `BoundF.prototype' to > the Object.create, or just a single object. var F = (function () {}).bind(obj); print(F.prototype); //undefined Object.create(F.prototype); //TypeError So as I said for creating instance you should explicit define `prototype' property and after that use, `Object.create'.
From: Dmitry A. Soshnikov on 15 Jun 2010 05:02 On 15.06.2010 12:48, Asen Bozhilov wrote: > Dmitry A. Soshnikov wrote: >> Asen Bozhilov wrote: > >>> Yes that is true, but is it too far? For example with built in >>> `Object.create' and `bind' you cannot create an instance by bound >>> function. Built-in `bind' return function object who does not have >>> `prototype' property. So in normal case `Object.create' should throw >>> TypeError. You should need explicitly define `prototype' property and >>> after that create an instance. >> >> What do you mean? Object.create accepts any object as a future prototype >> of the being created object. It doesn't matter, whether you define >> `prototype' property on bound function and pass `BoundF.prototype' to >> the Object.create, or just a single object. > > var F = (function () {}).bind(obj); > > print(F.prototype); //undefined > Object.create(F.prototype); //TypeError > > So as I said for creating instance you should explicit define > `prototype' property and after that use, `Object.create'. > Ah, but that's I said -- the fact that you pass F.prototype won't mean that you created an instance of _exactly_ F. You can do this simply passing any object, not necessary F.prototype: Object.create({ constructor: F }); That I meant. Dmitry.
From: Asen Bozhilov on 15 Jun 2010 05:17
Dmitry A. Soshnikov wrote: > Asen Bozhilov wrote: > > So as I said for creating instance you should explicit define > > `prototype' property and after that use, `Object.create'. > > Ah, but that's I said -- the fact that you pass F.prototype won't mean > that you created an instance of _exactly_ F. You can do this simply > passing any object, not necessary F.prototype: > > Object.create({ > constructor: F > > }); > > That I meant. Created object here is not an instance of `F'. See example: function F() { print(this instanceof F); } var obj = Object.create({constructor : F}); obj.constructor(); That object just has constructor property which refer `F' it is not instance of `F'. So in that case I will pass bounded thisValue to target function instead of treat as `new Bound();' |