Preventing instance extension

Juan Ignacio Dopazo dopazo.juan at gmail.com
Fri Nov 11 13:05:13 PST 2011


>
> On Fri, Nov 11, 2011 at 4:23 PM, Xavier MONTILLET <xavierm02.net at gmail.com
> > wrote:
>
Object.getPrototypeOf(this) === Point.prototype <=> this instanceof Point
>
> Actually, no. this instanceof Point => Object.getPrototypeOf(this) ===
Point.prototype, but not the other way around.


> And I don't understand your problem >_ <
>
> The problem is this that when you do prototypical inheritance using
constructors you usually call the superclass before any of the subclass
constructor code. So if you just seal(this) in the constructor of the
superclass the subclass can't add properties to the object.

function Person(name) {
  this.name = name;
  Object.seal(this);
  console.log(this instanceof Person, Object.getPrototypeOf(this) ===
Person.prototype); // true false when doing new Pirate()
}

function Pirate() {
  Person.apply(this, arguments);
  this.eyepatch = true;
}
Pirate.prototype = Object.create(Person.prototype);

var captain = new Pirate('Blackbeard');
console.log(captain.eyepatch); // undefined


Juan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111111/1ee0fec5/attachment.html>


More information about the es-discuss mailing list