it is a completely different from newObject object reference and after that if new2 returns the result of constructor the newObject would be just lost in the new2 scope.
So it have to be like
functionCar(doors,color){this.doors=doors;this.color=color;this.drive=()=>console.log('Vroom!');returnthis// equals to `newObject` ref}functionnew2(constructor,...constructorArgs){constnewObject={};Object.setPrototypeOf(newObject,constructor.prototype);returnconstructor.apply(newObject,constructorArgs);// bind newObject to constructor's `this` reference and call with args}
Ah okay, I see what you're saying now. I guess part of what I was trying to convey is that the constructor function can in fact return any object. In which case, the new function should just return that object as is.
What you've presented here is a more realistic edge case. I will think about a way to cover it in the article.
Great insights -- thank you.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
If
constructor
returnsit is a completely different from
newObject
object reference and after that ifnew2
returns the result ofconstructor
thenewObject
would be just lost in thenew2
scope.So it have to be like
Ah okay, I see what you're saying now. I guess part of what I was trying to convey is that the constructor function can in fact return any object. In which case, the new function should just return that object as is.
What you've presented here is a more realistic edge case. I will think about a way to cover it in the article.
Great insights -- thank you.