I am new to JavaScript and currently reading the book "JavaScript: The Good Parts". I am trying to understand the following:
function create(proto) {
var F = function() { };
F.prototype = proto;
return new F();
}
function create2(proto) {
var o = { };
o.prototype = proto;
return o;
}
var o = { }, c1 = create(o), c2 = create2(o);
o.status = 'OK';
document.writeln(c1.status);
document.writeln(c2.status);
create(proto)
is how this is done in the book.
create2(proto)
is how I thought it should work.
Obviously, the example from the book is working while mine is not, so the output is:
OK
undefined
Now my question is: Why does create2(proto)
not work like create(proto)
?