From 08158c5f7575125e76f70430d643680a9d984074 Mon Sep 17 00:00:00 2001 From: Mustafa Kemal Tuna <12192118+lumosmind@users.noreply.github.com> Date: Sat, 2 Nov 2019 09:23:46 +0300 Subject: [PATCH] not proved feature in article MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit some things has been mentioned about : "If user object changes while setTimout() waiting for run callback function which is method of user object, what will happen?" "What if before setTimeout triggers (there’s one second delay!) user changes value? Then, suddenly, it will call the wrong object!" And some things has been mentioned about "we will solve the problem with function binding" "The next solution guarantees that such thing won’t happen." But there is nothing to show that function binding really solves the problem which has been mentioned before. --- 1-js/06-advanced-functions/10-bind/article.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/1-js/06-advanced-functions/10-bind/article.md b/1-js/06-advanced-functions/10-bind/article.md index 20f2e098ff..96e38a40af 100644 --- a/1-js/06-advanced-functions/10-bind/article.md +++ b/1-js/06-advanced-functions/10-bind/article.md @@ -162,6 +162,10 @@ let sayHi = user.sayHi.bind(user); // (*) sayHi(); // Hello, John! setTimeout(sayHi, 1000); // Hello, John! + +// ...within 1 second +user = { sayHi() { alert("Another user in setTimeout!"); } }; +//setTimeout() is still using first user object definition ``` In the line `(*)` we take the method `user.sayHi` and bind it to `user`. The `sayHi` is a "bound" function, that can be called alone or passed to `setTimeout` -- doesn't matter, the context will be right.