From 98d5b3627472d10d114e657c744eb66eb29cebef Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 6 Aug 2023 18:31:52 +0300 Subject: [PATCH] ... Signed-off-by: Alex A. Naanou --- js-types-n-oop.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/js-types-n-oop.js b/js-types-n-oop.js index cfbdf41..a939d2b 100755 --- a/js-types-n-oop.js +++ b/js-types-n-oop.js @@ -295,7 +295,7 @@ // By convention constructor functions are capitalized (Pascal-case) // // Classic constructors are called with a "new" keyword which creates a -// bare instance and passes it to the function as the call context. +// bare instance and passes it to the function as the call context (this). // function A(){ @@ -314,8 +314,9 @@ // // 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: +// via the new syntax due to different contexts. This can be desirable +// in some cases but in general this is a pitfall, so let's unify the +// two cases: // function B(){ @@ -347,6 +348,23 @@ // Extending builtin types // +// The above approach will not work for "special" built-in objects, +// like functions, arrays and the like, because the created instance +// is a generic object and not a function, array or other built-in +// derived instance. +// We can avoid this by either creating the base instance manually +// or by using Reflect.construct(..). +// +// Here we'll extend Array: + + // XXX revise... + function List(){ + var obj = this instanceof List ? + this + : Reflect.construct(Array, arguments, List) + return obj + } + // XXX // Mixing builtin types