Given the following code, why doesn't (obj.foo)()
receive window
as this
? It seems to me like the parentheses are ignored, rather than treated as an expression that evaluates to foo
?
window.bar = 'window';
const obj = { bar: 'obj' };
obj.foo = function() {
console.log(`Called through ${this.bar}`);
}
obj.foo(); // Called through obj
(obj.foo)(); // Called through obj - Why?
(0, obj.foo)(); // Called through window
(true && obj.foo)(); // Called through window
Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. s consists of parentheses only ' () [] {}'. An interesting property about a valid parenthesis expression is that a sub-expression of a valid expression should also be a valid expression. (Not every sub-expression) e.g.
Valid Parentheses. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Note that an empty string is also considered valid.
A set of parenthesis are duplicate if the same subexpression is surrounded by multiple parenthesis.
If the number of characters encountered between the opening and closing parenthesis pair, which is equal to the value of the counter, is less than 1, then a pair of duplicate parenthesis is found else there is no occurrence of redundant parenthesis pairs. For example, ( ( (a+b))+c) has duplicate brackets around “a+b”.
They're not ignored, but the result of (obj.foo)
is the same Reference (specification term) as the result of obj.foo
(see this section in the spec). So the property accessor has the same information to work with to get the object and property and to use that object as this
.
Cause obj.foo
evaluates to a Reference that contains obj
and "foo"
. When you [[Call]] the reference, both obj
and foo
are known as part of the Reference.
Most operators (including the comma operator) evaluate the Reference to a value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With