Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2023-06-14 00:50:35 +03:00
parent ea39e3d0c3
commit 98eedb02bc

View File

@ -119,6 +119,7 @@
// Prototypes and inheritance // Prototypes and inheritance
// //
// XXX
var a = { var a = {
} }
@ -154,7 +155,12 @@
__proto__: A.prototype, __proto__: A.prototype,
} }
// XXX a safer way -- now we can forget new... //
// The problem with the default way this is done is that now a
// constructor will behave differently when called directly or if called
// via the new syntax. This can be desirable in some cases but in
// general this is a pitfall, so let's unify the two cases:
//
function B(){ function B(){
var obj = { var obj = {
@ -163,10 +169,10 @@
return obj return obj
} }
// this can be calles with and withot new // this can be called with and without new:
var z = B() var z = B()
// less naive... // less naive -- reuses the instance created by new...
function C(){ function C(){
var obj = this instanceof C ? var obj = this instanceof C ?
this this
@ -176,6 +182,11 @@
// make C instances related to B... // make C instances related to B...
C.prototype.__proto__ = B.prototype C.prototype.__proto__ = B.prototype
//
// Note that constructor extension is trivial if you think of how
// prototypical inheritance works, to link A and B "instances" all we
// needed to do is link the constructor prototypes in the code above.
//
// Extending builtin types // Extending builtin types