From 2f757ba3f17d7f97bf5f921d70b592840749cfb6 Mon Sep 17 00:00:00 2001 From: Abhas Bhattacharya Date: Fri, 3 Apr 2020 09:58:08 +0530 Subject: [PATCH] update post: What is `this` inside foo.bar()? --- ck8dzlitm01atxjs1322jz9a2.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ck8dzlitm01atxjs1322jz9a2.md b/ck8dzlitm01atxjs1322jz9a2.md index c62de65..43c2eb6 100644 --- a/ck8dzlitm01atxjs1322jz9a2.md +++ b/ck8dzlitm01atxjs1322jz9a2.md @@ -71,24 +71,22 @@ TLDR - If you would like to see me actually go through the spec, this video migh ![IsPropertyReference- reference where base is object or primitive](https://cdn.hashnode.com/res/hashnode/image/upload/v1584625822167/ZmBSyVj95.png) -Link - 1. https://tc39.es/ecma262/#sec-evaluatecall, 2. https://tc39.es/ecma262/#sec-ispropertyreference - ### PropertyReference This `foo.bar` structure is defined in the spec as a [PropertyReference](https://tc39.es/ecma262/#sec-ispropertyreference). -Think of PropertyReference as anything that is valid as the left side of an assignment expression, if you were assigning to the property of a object or primitive. +If you were assigning a value to the property of a object or primitive, anything that is valid as the Left Hand side of the assignment expression is a PropertyReference. -PropertyReference = Reference + base is object or primitive +So, PropertyReference = Reference + base is object or primitive ![PropertyReference illustration](https://cdn.hashnode.com/res/hashnode/image/upload/v1585431425436/5XBWmcI5y.png) -Some examples of valid and invalid PropertyReference 👇 +Some examples of valid and invalid PropertyReference might make it more clear - 👇 %[https://gist.github.com/bendtherules/3467664a9c567617342b73c3681c1dc7] ### Steps 1. If it is a PropertyReference, then set `thisValue` to base of the PropertyReference (i.e. `foo` in `foo.bar`). -2. Or else, take thisValue from surrounding Environment Record. +2. Or else, thisValue is `undefined`. The obtained `thisValue` is forwarded to the abstract operation [Call](https://tc39.es/ecma262/#sec-call), which in turn verifies that the resolved value of `foo.bar` is a function and then calls the internal method [function.[[Call]]](https://tc39.es/ecma262/#sec-built-in-function-objects-call-thisargument-argumentslist) with the same thisValue. This [[call]] interface is very similar to the `function.call` public interface and can be approximated with that. One small diff. we noted earlier is that [[call]] changes `this` value from undefined to global object in non-strict mode.