Creating a JavaScript object without the new keyword (davecooper.org) 3 points by gurgus 10y ago ↗ HN
[–] dskloet 10y ago ↗ p2 is not an instance of Person. You also need to make sure p2's constructor is set to Person. [–] gurgus 10y ago ↗ Interesting - how can I go about this? [–] dskloet 10y ago ↗ p2.constructor = Person;Without that, the object also doesn't have the right prototype. [–] gurgus 10y ago ↗ Thanks! Will update my post and attribute you with that! [–] gurgus 10y ago ↗ All fixed up now. Thanks for the feedback :) I ended up setting __proto__ directly so that an instanceof test passes. [–] dskloet 10y ago ↗ Looks like that didn't work either and you ended up with Object.create which is a fairly new function. I think assigning Person.prototype (instead of Person) to p2.__proto__ would have worked. Or is that already what you had before?
[–] gurgus 10y ago ↗ Interesting - how can I go about this? [–] dskloet 10y ago ↗ p2.constructor = Person;Without that, the object also doesn't have the right prototype. [–] gurgus 10y ago ↗ Thanks! Will update my post and attribute you with that! [–] gurgus 10y ago ↗ All fixed up now. Thanks for the feedback :) I ended up setting __proto__ directly so that an instanceof test passes. [–] dskloet 10y ago ↗ Looks like that didn't work either and you ended up with Object.create which is a fairly new function. I think assigning Person.prototype (instead of Person) to p2.__proto__ would have worked. Or is that already what you had before?
[–] dskloet 10y ago ↗ p2.constructor = Person;Without that, the object also doesn't have the right prototype. [–] gurgus 10y ago ↗ Thanks! Will update my post and attribute you with that! [–] gurgus 10y ago ↗ All fixed up now. Thanks for the feedback :) I ended up setting __proto__ directly so that an instanceof test passes. [–] dskloet 10y ago ↗ Looks like that didn't work either and you ended up with Object.create which is a fairly new function. I think assigning Person.prototype (instead of Person) to p2.__proto__ would have worked. Or is that already what you had before?
[–] gurgus 10y ago ↗ All fixed up now. Thanks for the feedback :) I ended up setting __proto__ directly so that an instanceof test passes. [–] dskloet 10y ago ↗ Looks like that didn't work either and you ended up with Object.create which is a fairly new function. I think assigning Person.prototype (instead of Person) to p2.__proto__ would have worked. Or is that already what you had before?
[–] dskloet 10y ago ↗ Looks like that didn't work either and you ended up with Object.create which is a fairly new function. I think assigning Person.prototype (instead of Person) to p2.__proto__ would have worked. Or is that already what you had before?
6 comments
[ 3.0 ms ] story [ 29.2 ms ] threadWithout that, the object also doesn't have the right prototype.