Setting a constructor's prototype property to a primative, array or function.

P

Peter Michaux

Hi,

What happens if you do this?

function Person() {}
Person.prototype = 7;
var ted = new Person();

Also what happens if you set Person.prototype to an array or function?

Thank you,
Peter
 
R

Richard Cornford

Peter said:
What happens if you do this?

function Person() {}
Person.prototype = 7;
var ted = new Person();

See ECMA 262, 3rd Ed. Section 13.2.2

If the - prototype - property of a function that is used as a constructor
is not an object (of the ECMAScript Object type) then the original value
of - Object.prototype - is used to set the [[Prototype]] if the objects
constructed.
Also what happens if you set Person.prototype to an array
or function?

Functions and Arrays are objects so they will be used as the
[[Prototype]] of the newly constructed object, and it will inherit their
properties and methods.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top