I rather like the prototype way of programming and have been trying to understand it in javascript.
I saw this bit of code in The Good Parts:
function beget(o){
function F(){
F.prototype = o;
};
return new F();
};
I don’t get this at all lol. If all you have to do is set the prototype to a past object, then couldn’t you just do this:
var parent = {
num = 66;
};
var child = {
prototype: parent
};
This doesn’t seem to work though, cause child.num is returned as undefined. How do you describe javascript prototype programming and what are your methods?
Thanks guys
You can only add
prototypeto a function object. When invoked vianewit will use it as its prototype.Btw, the function you quoted is part of the new version of ECMAScript as
Object.create(with an additionalpropertiesObjectparameter).Let me put it this way:
{object}is a singleton. Afunction() objecthowever is a constructor, that is, when called withnewcreates an instance (by executing the function’s body, and using the constructor’s prototype). Of course the prototype can be a function object too, and have its own prototype, etc. Specialization and Generalization means walking up and down the prototype chain.