Quote:
Originally Posted by suzzer99
Lol here's one of the problems. I plugged the output into JS fiddle but don't understand why I get the result = 0 then 2. Anyone?
Code:
var length = 10;
function fn() {
console.log(this.length);
}
var obj = {
length: 5,
method: function(fn) {
fn();
arguments[0]();
}
};
obj.method(fn, 1);
What is the Output?
Ok, I haven't read past this post.
I'm gonna write out what I think and hopefully people will explain where I am wrong in detail because I feel like I'm about to learn a lot from this and I'm pretty excited.
Code:
var length = 10;
function fn() {
console.log(this.length);
}
We're creating a global called length which is equal to the javascript integer 10.
We're creating a function that console logs this.length. I am immediately noting that "this" is being used in a weird way here. This is a code style that I haven't seen and is obviously trying to trick us. Because we aren't in an object, or anything, I feel like currently "this" is just pointing to fn(), so length if we executed this function right now would be undefined.
Code:
var obj = {
length: 5,
method: function(fn) {
fn();
arguments[0]();
}
};
Ok now we got an object. We have fn() all over this place, so the game is on, here is where "this" is gonna shine.
Length is 5, so I'm thinking right now that in execution context, we just got our "this.length" for our fn().
Ok so let's figure out our output.
We are invoking method which is:
Code:
function(fn) {
fn();
arguments[0]();
}
This is a method that takes 1 argument, fn and hopefully its a function because we're about to execute it. It is a function so it is executed. We also pass in a second argument to this method, even tho its signature only calls for one, and it doesn't return a function (I think technically that the "console.log(this.length)" of fn() returns undefined) so I'm really not sure wtf this second argument is doing here, again this is some weird trivia ****.
So we are executing fn() and we are seemingly logging 5, but that doesn't seem like the full story. We have this 1 we have to deal with and we have arguments index zero being executed.
Now, fn() doesn't have any type of explicit or implicit return (I don't think), so I feel pretty good about arguments[0]() running, but what it is about to do, I have honestly no ****ing clue.
At this point I could guess, but I don't even know what I should guess at, I'm gonna spend 10-15 seconds googling "arguments[0]" and just see if its some reserved word with special meaning/usage.
Ok I'm back that was like 5 seconds, praise be to google.
So arguments is an array of the arguments passed to a function in non arrow functions.
Ok I'm going back to MDN. I added a function fn() { console.log(5) } and func(1, 2, fn() ) to the arguments example and it printed out my console log first and then the other arguments. It is like my "fn()" was hoisted to the front of the arguments array. That seems cool, but I have no idea why that is happening. And I'm not sure how that helps me here.
I went back to MDN and I changed my fn to:
Code:
function fn() {
setTimeout( function() {
console.log(1000)
},10 )
}
That outputs: 1, 2, undefined, 1000.
So I'm gonna guess that we get:
"undefined, 5*"
as the output.
Last edited by Larry Legend; 08-03-2018 at 02:17 AM.
Reason: meant 5