From 8d69ef158f78d88d8081a91b2bbc1b04a7cf6bed Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Thu, 19 Jul 2018 23:39:01 +0300 Subject: [PATCH] closes #441 --- .../06-constructor-new/article.md | 18 ++++++++++++++---- .../_js.view/solution.js | 0 .../6-calculator-extendable}/_js.view/test.js | 0 .../6-calculator-extendable}/solution.md | 0 .../04-array/6-calculator-extendable}/task.md | 0 5 files changed, 14 insertions(+), 4 deletions(-) rename 1-js/{04-object-basics/06-constructor-new/4-calculator-extendable => 05-data-types/04-array/6-calculator-extendable}/_js.view/solution.js (100%) rename 1-js/{04-object-basics/06-constructor-new/4-calculator-extendable => 05-data-types/04-array/6-calculator-extendable}/_js.view/test.js (100%) rename 1-js/{04-object-basics/06-constructor-new/4-calculator-extendable => 05-data-types/04-array/6-calculator-extendable}/solution.md (100%) rename 1-js/{04-object-basics/06-constructor-new/4-calculator-extendable => 05-data-types/04-array/6-calculator-extendable}/task.md (100%) diff --git a/1-js/04-object-basics/06-constructor-new/article.md b/1-js/04-object-basics/06-constructor-new/article.md index ae747d36..5fe6e5d9 100644 --- a/1-js/04-object-basics/06-constructor-new/article.md +++ b/1-js/04-object-basics/06-constructor-new/article.md @@ -85,6 +85,10 @@ The constructor can't be called again, because it is not saved anywhere, just cr ## Dual-syntax constructors: new.target +```smart header="Advanced stuff" +The syntax from this section is rarely used, skip it unless you want to know everything. +``` + Inside a function, we can check whether it was called with `new` or without it, using a special `new.target` property. It is empty for regular calls and equals the function if called with `new`: @@ -94,14 +98,18 @@ function User() { alert(new.target); } -// without new: +// without "new": +*!* User(); // undefined +*/!* -// with new: +// with "new": +*!* new User(); // function User { ... } +*/!* ``` -That can be used to allow both `new` and regular syntax to work the same: +That can be used to allow both `new` and regular calls to work the same. That is, create the same object: ```js run function User(name) { @@ -116,7 +124,9 @@ let john = User("John"); // redirects call to new User alert(john.name); // John ``` -This approach is sometimes used in libraries to make the syntax more flexible. Probably not a good thing to use everywhere though, because omitting `new` makes it a bit less obvious what's going on. With `new` we all know that the new object is being created, that's a good thing. +This approach is sometimes used in libraries to make the syntax more flexible. So that people may call the function with or without `new`, and it still works. + +Probably not a good thing to use everywhere though, because omitting `new` makes it a bit less obvious what's going on. With `new` we all know that the new object is being created. ## Return from constructors diff --git a/1-js/04-object-basics/06-constructor-new/4-calculator-extendable/_js.view/solution.js b/1-js/05-data-types/04-array/6-calculator-extendable/_js.view/solution.js similarity index 100% rename from 1-js/04-object-basics/06-constructor-new/4-calculator-extendable/_js.view/solution.js rename to 1-js/05-data-types/04-array/6-calculator-extendable/_js.view/solution.js diff --git a/1-js/04-object-basics/06-constructor-new/4-calculator-extendable/_js.view/test.js b/1-js/05-data-types/04-array/6-calculator-extendable/_js.view/test.js similarity index 100% rename from 1-js/04-object-basics/06-constructor-new/4-calculator-extendable/_js.view/test.js rename to 1-js/05-data-types/04-array/6-calculator-extendable/_js.view/test.js diff --git a/1-js/04-object-basics/06-constructor-new/4-calculator-extendable/solution.md b/1-js/05-data-types/04-array/6-calculator-extendable/solution.md similarity index 100% rename from 1-js/04-object-basics/06-constructor-new/4-calculator-extendable/solution.md rename to 1-js/05-data-types/04-array/6-calculator-extendable/solution.md diff --git a/1-js/04-object-basics/06-constructor-new/4-calculator-extendable/task.md b/1-js/05-data-types/04-array/6-calculator-extendable/task.md similarity index 100% rename from 1-js/04-object-basics/06-constructor-new/4-calculator-extendable/task.md rename to 1-js/05-data-types/04-array/6-calculator-extendable/task.md