16 Respostas
+ 6
Hafsa Hussein Mohammed I agree with the explanations provided by ~ swim ~ and Gordon. Recapping Problem 1: By default, in Javascript, the context of `this` belongs to the relative parent scope from where the function is invoked. var go = fun.day.bar; Here, the relative parent scope of the variable `go` will be different from that of `bar` and will be reflected as such when invoking the respective functions. Therefore, `go()` will access the value `x` defined in its relative parent, the global scope, which is 3. Likewise, when calling `fun.day.bar()`, the relative parent is the `day` object, where the value `x` is 1. (continue...)
16th May 2019, 7:59 PM
David Carroll
David Carroll - avatar
+ 8
Gordon I saw that too. But, I assumed Javascript will cast it to number. ¯\_(ツ)_/¯
17th May 2019, 4:27 AM
David Carroll
David Carroll - avatar
+ 7
The all game is of 'this' keyword. When you store the function 'bar' , in 'go' variable of window object, the 'this' object will hold the window object. Then you know, var x = 3 console.log(window.x==x); Inside go function, this==window So, this.x==x. I think the remaining part of explanation, you can imagine, think yourself. Gordon David Carroll ~ swim ~ I properly agreed to your answer.
17th May 2019, 1:19 AM
Sarthak Pokhrel
Sarthak Pokhrel - avatar
+ 7
SPACE David Carroll Gordon ~ swim ~ thanks all for the helpful answers
17th May 2019, 1:25 AM
Hafsa Mohamed
Hafsa Mohamed - avatar
+ 6
Recapping Problem 2: A new, readonly property named `-1` is defined on `Array.prototype`. The getter for this new property returns the last index position of the current array instance. An easier way to read this would be to replace `-1` with "lastValue" in this code. Then the code would make more sense where either of the following lines would return 4: //Property accessor via key. //Same as `a[-1]` a["lastValue"] //Property accessor via dot notation. a.lastValue NOTE: No equivalent exists for the second example since `a.-1` is invalid. Properties cannot be accessed via the dot notation if their name begins with a numeric or operator character. Therefore, only the key accessor is valid.
16th May 2019, 8:07 PM
David Carroll
David Carroll - avatar
16th May 2019, 12:29 PM
Hafsa Mohamed
Hafsa Mohamed - avatar
+ 5
Question 2 - Array is a type of Object - We can use square bracket as well as dot to access Object property - define property defines the getter of -1 in a Python-alike manner, returning the last element in an array. https://www.sololearn.com/post/46592/?ref=app https://code.sololearn.com/Wyr76080kKxS/?ref=app
16th May 2019, 3:52 PM
Gordon
Gordon - avatar
16th May 2019, 3:50 PM
Gordon
Gordon - avatar
+ 4
David Carroll funny thing is happening in [] 🧐 https://code.sololearn.com/WBqcWEVjEJ3z/?ref=app i can arr["0"] đŸ€” i guess you are right, "-1" is treated as -1
17th May 2019, 4:41 AM
Gordon
Gordon - avatar
+ 3
i just found this experiment of define getter by SPACE https://code.sololearn.com/WgqQ01BXR3Db/?ref=app doing something similar, just using another syntax
16th May 2019, 4:02 PM
Gordon
Gordon - avatar
+ 3
for getter setter, this is simple and clear demo https://code.sololearn.com/WuswYAzn9MWs/?ref=app
16th May 2019, 4:04 PM
Gordon
Gordon - avatar
+ 3
This demo is "array is a type of object" https://code.sololearn.com/WdpzC6Y7zBa6/?ref=app
16th May 2019, 4:07 PM
Gordon
Gordon - avatar
+ 3
in question 2, come to think of it, in defineProperty, it's "-1" (a string), but in accessing, it's -1 (int), so what's the underlying typecast? int to string or string to int? when did it happen?
17th May 2019, 3:16 AM
Gordon
Gordon - avatar