update post: What is this inside foo.bar()?

This commit is contained in:
2020-04-03 09:58:08 +05:30
parent 8e1c2a9566
commit 2f757ba3f1
+4 -6
View File
@@ -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) ![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 ### PropertyReference
This `foo.bar` structure is defined in the spec as a [PropertyReference](https://tc39.es/ecma262/#sec-ispropertyreference). 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) ![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] %[https://gist.github.com/bendtherules/3467664a9c567617342b73c3681c1dc7]
### Steps ### Steps
1. If it is a PropertyReference, then set `thisValue` to base of the PropertyReference (i.e. `foo` in `foo.bar`). 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. 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. 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.