primitives will simply get ignored when assigned as a prototype. is this right? #89

Closed
idkiller opened this Issue Jul 14, 2011 · 5 comments

Comments

Projects
None yet
4 participants

in chapter "The prototype"

The Prototype Property

While the prototype property is used by the language to build the prototype chains, it is still possible to assign any given value to it. Although primitives will simply get ignored when assigned as a prototype.

is this really right?

when I test at firefox and chrome, assigned primitives really change prototype.

function foo() { this.x = 10; }

function bar() {}

bar.prototype = new foo();

bar.prototype = 1; // it was not ignored. prototype has changed really.

@idkiller Here you're changing a property, not the prototype chain. prototype is just a simple property, you can assign to it any value you want and it will just work. The magic of this property, however, comes when you use the new keyword to construct an object from a constructor function, which will make a fresh object with the prototype chain described by the constructor.

Now, here comes the catch: the actual prototype is a hidden property called [[Prototype]]. The specs don't require any implementation to expose it, and most don't do so. Some (like v8 and SpiderMonkey) expose this hidden property, usually as __proto__.

[[Prototype]] only accepts an Object or null, as you can see from the specs here: http://es5.github.com/#x8.6.2

And here is the proof:

>> var base = {a: 1}
>> var other = Object.create(base) // constructs a new object with [[Prototype]] === base
>> other.__proto__ === base
true
>> other.a
1
>> other.__proto__ = 'foo'
>> other.__proto__ === base
true
>> other.a
1
Collaborator

timruffles commented Oct 9, 2013

Yes @killdream is quite correct - you can set an explicit prototype property, but to the interpreter this has nothing to do with the object's prototype. Simple huh? :)

However, the text is really not explicit enough around this. I will update it.

@timruffles timruffles closed this Apr 30, 2014

evverx commented Apr 5, 2015

However, primitives will simply get ignored when assigned as a prototype.
function Foo() {}
Foo.prototype = 1; // no effect

Really?:)

function Foo() {}
(new Foo).constructor === Foo;
function Foo() {}
Foo.prototype = 1;
(new Foo).constructor === Object;

but who cares? :)

The default Foo.prototype object by default gets a public, non-enumerable property called .constructor, and this property is a reference back to the function (Foo in this case) that the object is associated with. See Creating Function Objects

Foo.prototype = 1 sets prototype property to non-object value.
Quote from ES5 Construct:

If Type(proto) is not Object, set the [[Prototype]] internal property of obj to the standard built-in Object prototype object as described in 15.2.4.
Collaborator

timruffles commented Apr 5, 2015

PRs always welcome :)

On Sunday, 5 April 2015, Evgeny Vereshchagin notifications@github.com
wrote:

However, primitives will simply get ignored when assigned as a prototype.

function Foo() {}Foo.prototype = 1; // no effect

Really?:)

function Foo() {}
(new Foo).constructor === Foo;

function Foo() {}Foo.prototype = 1;
(new Foo).constructor === Object;

but who cares? :)

The default Foo.prototype object by default gets a public, non-enumerable
property called .constructor, and this property is a reference back to
the function (Foo in this case) that the object is associated with. See Creating
Function Objects http://es5.github.io/#x13.2

Foo.prototype = 1 sets prototype property to non-object value.
Quote from ES5 Construct http://es5.github.io/#x13.2.2:

If Type(proto) is not Object, set the [[Prototype]] internal property of obj to the standard built-in Object prototype object as described in 15.2.4.


Reply to this email directly or view it on GitHub
#89 (comment)
.

evverx commented Apr 6, 2015

Yes, I know :) I'm reading and collecting inaccuracies now. Maybe I'll be back with PRs :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment