+ 5
Any help please?
https://www.sololearn.com/post/99607/?ref=app Also why its 4 https://www.sololearn.com/post/99613/?ref=app
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...)
+ 8
Gordon I saw that too. But, I assumed Javascript will cast it to number.
ÂŻ\_(ă)_/ÂŻ
+ 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.
+ 7
+ 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.
+ 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
+ 4
Closures
https://www.sololearn.com/post/91113/?ref=app
swim's right
+ 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
+ 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
+ 3
for getter setter, this is simple and clear demo
https://code.sololearn.com/WuswYAzn9MWs/?ref=app
+ 3
This demo is "array is a type of object"
https://code.sololearn.com/WdpzC6Y7zBa6/?ref=app
+ 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?