The 3rd question really inspired me a lot. thanks!
We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
in question
Explain the value of "this"
you need to add "use strict"
without "use strict" this will be equal to global window object.
without "use strict" mode(method = obj.go)(); // (3) [object Object]
(obj.go || obj.stop)(); // (4) [object Object]
Awesome tutorial....I went back to mdn to learn more and this made mdn very very friendly...since mdn is not really a go to for total beginners (my opinion)
In JavaScript this is “free”, its value is evaluated at call-time and does not depend on where the method was declared, but rather on what object is “before the dot”. - This statement made me understand question 3, i had to review the tutorial after failing the test but atleat now i have the idea. Thanks soo much for this tutorial
In the first task it should be mentioned that the code is running in strict mode. Otherwise it won't throw an error, because makeUser's this, would be global object
It is a subtle detail and I hope writers updates the question accordingly.
Hello.
I don't understand why my method onClick_2 don't return undefined. Why it remember context (button_1)?
Why do I have:
"OK" when is used button_1.onClick_2()
"OK" when is used button_2.onClick_2()
function Button(value) {
this.value = value;
this.onClick_1 = function () {
console.log(this.value);
};
this.onClick_2 = () => {
console.log(this.value);
};
};
let button_1 = new Button("OK");
let button_2 = { value: 'Cancel' };
button_2.onClick_1 = button_1.onClick_1;
button_2.onClick_2 = button_1.onClick_2;
button_1.onClick_1(); // console >> "OK"- it's clear for me
button_1.onClick_2(); // console >> "OK" - Why "OK" not "undefined"
button_2.onClick_1(); // console >> "Cancel"- it's clear for me
button_2.onClick_2(); // console >> "OK" - Why "OK" not "undefined" or "Cancel"
in button_1.onClick_2() function call 'this' keyword is referring to the new object whose reference you have stored in button_1. So it simply returns value of that object which is "OK", that is why it is not "undefined" as it is referring to object.
Again, in button_2.onClick_2() function call 'this' keyword is referring to the same new object whose value is 'OK' so you are getting 'OK' not 'undefined' or 'cancel'.
You can check it by adding console.log(this) inside onclick functions.
hope it helps!
My most important question.
Where does an arrow function used as a class method store context?
Arrow function doesn't create own context. When I use object
button_1 = {
value: 'OK',
onClick_2: () => { console.log(this.value);
}
button_1.onClick_2(); // undefined
arrow function doesn't have context and i see undefined.
The same example but done with the "new" operator works differently.
In these cases arrow function has context. Where does it remember it if doesn't create it.
function Button(value) {
this.value = value;
this.onClick_2 = () => {
console.log(this.value);
};
};
button_1=new Button('OK')
button_1.onClick_2(); // OK
class Button {
constructor(value){
this.value = value;
};
onClick_2 = () => {
console.log(this.value);
};
};
button_1=new Button('OK')
button_1.onClick_2(); // OK
On a Function definition This is assigned according to where the function is called, in contrary the this inside an arrow function is assigned according to in wich context it is defined ( whether inside global context or a class).
3rd question is amazing because to solve this question have to thinking a lot of.
They wanted to use the object again after any function call
Normally to do multiple calls we would have to do
ladder.up(); // increments returns undefined (nothing)
ladder.down(); // decrements returns undefined
....
in-short we have to reference object again and again.
So, way to make nested calls would be possible if function returned its object.
which is why he wrote return this at end of every function in solution.
Now, when we would use ladder.up(); // increments returns this (its object)
so now you can chain nested calls as each function would be returning its object reference.
ladder.up().down().showStep();
I don't remember if it has been explained in a previous chapter of the tutorial: every function call returns something; if your function does not explicitly return a value, then it will return undefined.
You can see that by opening the console and running console.log('something'): you'll then receive two lines as a result: one with the content of the function call (in this case, the word "something"), and, in the second line, it will show undefined, because console.log does not return anything.
What you have to do in the exercise is to allow the function to be called over and over again, with object notation, like obj.func1().func2().func3().... This is called chaining. To do so, the object must be reachable after every function call.
But I agree that this is a more advanced way of thinking the language -- the process itself is quite simple, but to get to it requires some more knowledge.
The last question was so poorly worded that I think MANY brand new JS coders (including myself) have no idea what they are even asking. The people who write these exercises suffer from The Curse of Knowledge.
SUPER THANK YOU, THE BEST WEBSITE RESOURCES FOR JAVASCRIPT , PERIOD.
The explanation for the first task makes absoluteley no sense, if you haven't read the next chapter about constructor functions.
If I litereally had no idea what the chaining question was even asking me do I need to go back and learn something. Who else got what it was asking?
Can we use arrow function? Will it recognize "this"? Like
showStep = () => {
alert( this.step );
};
Or
showStep : () => {
alert( this.step );
};
Ps found the answer here: https://stackoverflow.com/q...
Still a question what does this lexically enclosing context mean? How exactly do these arrow functions operate and why?
And in case of defining like a normal method it's said they point to the start of an object declaration?
Arrow functions operates same as normal function declaration..Syntax is Different
// function Declaration
function sum(a, b) {
return a+b
}
// arrow function
let arrow = (a, b) => a + b
// function name, (a,b) is function parameters, (a+b) is function statement.
// For parameters we can omit parenthesis if there is exactly one parameter, not less, not more..means if there is no parameters, or more than one parameter then parenthesis is must..
// If you want to write multi line statements then use
let arrow = (a, b) => {
let sum = a + b
return sum
}
//In multi line statements,, use of return is must otherwise function will not return anything.
//Arrow Function does not use 'this', if used, refere to the outer function/global window object.
In the Chaining exercise if you remove the return this; from the showStep() method it returns the same result, wonder why its required since we are calling it last just to show the result.
Can anyone explain this?
'return this' was added to showStep() just in case you have to change the chain order and call showStep() somewhere in between and not as the last item. For example, if there is no 'return this' added to showStep(), if you made a chain like this 'ladder.up().showStep().down()', you'd get an error.
Yes,in this situation there is no need in
return this;
, but if you try something like this ladder.up().showStep().down().showStep(); It won't work without returning the object
If we remove return this then in chaining we get error : Cannot read property 'down' of undefined
use the plunker to experiment, the return this; in showStep() is not needed since its the last call in the chain
You are right. It's not needed for that particular call but it may be needed for different call context.
Calculator question and the chaining one is nice.
It's interesting how you can give an object a property via a method just by using 'this'
let a = {
a() { this.name = 'cat' },
b() { this.age = 5 }
}
a.a() // a now has a name property
a.b() // a now has an age property
console.log(a) //{a: ƒ, b: ƒ, name: "cat", age: 5}
But can only be done if you represent the new property in a function as a() and b() in the question. That's explicitly why "this" can work.
That last exercise was bullshit. How am I supposed to know that is the answer?? It isn't explained anywhere. You may be good programmers but you are all terrible teachers. Please take a pedagogy class.
Keep calm pal.This is one of the finest blogs on JS. Things are explained in a very good way, systematically. And to talk about the last exercise, you just need to think : all the methods of an object can only be called on that particular object, so each method must return the object itself. That's it.
To be fair, most classes in any field are like this. Questions like that make you think. They make you research. I'm sure you've been in school, just like anyone else. Have your teachers spoonfed you throughout your studies? I highly doubt it, unless you went to horrible schools. They usually make you think by giving you hard problems that make you actually have to learn, rather than regurgitate memorized facts. Long story short, use Google. Use DuckDuckGo. Use StackOverflow. Actually do work. As is normally said within the cybersecurity industry, if you can't figure a problem out, then try harder. The answer is always within reach.
It's an awesome question .. maybe you are just terrible student ;)
Lol, you are a terible student, i have been teach developlent for the past 5 years. This is by far one of the best JS block I ever encounter. You just like one student that is in every batch of my bootcamp that act he is smart but he is actualy dumb.