News for hackers. Stuff that matters.
Re:Understanding "Prototypes" in JavaScript by Cushman on Sunday August 14, 2011 @01:36PM (#2884033)
Here's the part I don't get: `Object.create`, as it's defined there, is a long-winded, roundabout way of saying

    {__proto__: o}
People seem to think that this is functionality that was lacking from JavaScript; many of these same people have some inexplicable distaste for the (non-standard) `__proto__` property. What gives?

(Okay, technically it's {__proto__: o, constructor: function F() {}}...)

Re:Understanding "Prototypes" in JavaScript by alunny on Sunday August 14, 2011 @02:40PM (#2884148)
As I understand it, the main objection to `__proto__` is that it's writable, and thus not atomic with object creation. This creates a bunch of headaches for implementors for performance and security.

More knowledgable people than myself discuss it on this es-discuss thread: http://www.mail-archive.com/[email protected]/msg06142....

There is a Harmony proposal for a "set prototype of operator" that would solve this problem; instead of writing:

    var newObj = {__proto__: OldObject, id: 5}
You would write:

    var newObj = OldObject <| { id: 5 }
Detailed here: http://wiki.ecmascript.org/doku.php?id=harmony:proto_operato...
Re:Understanding "Prototypes" in JavaScript by Cushman on Sunday August 14, 2011 @09:21PM (#2885040)
Yeah, I can understand how it makes implementors' lives easier... I just like it when they deal with annoying stuff so that I don't have to :)

I know people don't use mutable __proto__ for anything useful, but it does enable some neat tricks. Personally I wish implementors would embrace it and optimize for it rather than trying to make it go away.

As for the "prototype for" operator... that's disgusting.

Re:Understanding "Prototypes" in JavaScript by shmageggy on Sunday August 14, 2011 @01:40AM (#2882887)
I like how you illustrate in a clear way something that is unintuitive for people coming from a more traditional class-based background; the scoping of this.

It's easy to see how it applies locally to descendants of the original object, such as here:

  var person = Object.create(null);
  defineProperty(person, 'fullName', function() {
    return this.firstName + ' ' + this.lastName;
  });
where neither fullname nor person even have those properties, but their descendents do.
Re:Understanding "Prototypes" in JavaScript by Cardinal on Sunday August 14, 2011 @01:47PM (#2884061)
I have a Java background and I don't really use JavaScript, but I'd like to understand something :

var man = fromPrototype(person, { sex: "male" });

After this code runs, we have a reference to a function (man) inside which we declare an object (newObject) for which we define the new property (sex). That means we can now call newObject.sex

But later he's calling that property on the function itself : jeremy.sex

How is this possible?

Re:Understanding "Prototypes" in JavaScript by cdsanchez on Sunday August 14, 2011 @02:51PM (#2884177)
His example has an error. He forgot to return newObject at the end of the fromPrototype function. The fromPrototype function returns an instance (an object) that inherits from the given prototype and with the same properties you passed in with the object.
Re:Understanding "Prototypes" in JavaScript by Cardinal on Sunday August 14, 2011 @03:23PM (#2884254)
Thanks.
Re:Understanding "Prototypes" in JavaScript by aes on Sunday August 14, 2011 @03:10PM (#2884222)
It seems that fromPrototype is missing a line:

  return newObject;
'man' and 'jeremy' are objects, not functions.
Re:Understanding "Prototypes" in JavaScript by wqfeng on Sunday August 14, 2011 @11:01PM (#2885258)
I like the author's writing style very much. It's the best explanation on prototype I've ever read. His another article on this is great too.