The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

jeroend (23) [Avatar] Offline
#1
I'm really learning something about Javascript with this almost academic book while trying to apply all its knowledge to my own project.
As a Java developer I'm very much interested in Javascripts' inheritance and hence my close study of chapter 5. The possibility to mimic inheritance with properties as explained in 5.4 is quite compelling. But to my great surprise I also found out that:

Ninja.prototype = new Person();

does not only make Ninja inherit person's prototyped functions but also person's properties:

function Person(){ this.danced = 0;};
Person.prototype.dance = function(){ this.danced++;};
function Ninja(){}
Ninja.prototype = new Person();
var ninja = new Ninja();
ninja.dance();
assert(ninja.danced == 1);

I think that this feature is too nice not to mention, unless I'm missing some very obvious nasty side effects.
jeroend (23) [Avatar] Offline
#2
Re: 5.4 Achieving inheritance with prototypes
apparently this works when properties are simple javascript types.
see http://stackoverflow.com/q/8724427/288190
bear.bibeault (675) [Avatar] Offline
#3
Re: 5.4 Achieving inheritance with prototypes
Naturally, any code in the constructor function will be executed.