One interview question

var length = 10
function fn(){
    alert(this.length)
}
var obj = {
    length: 5,
    method: function(fn) {
        fn() // ?
        arguments[0]() // ?
    }
}
obj.method(fn)

I attended a junior interview and then i saw the reference answer:arguments[0] equivalent to fn .
Can someone explain that, I am a newbie, thank you.

Hi there, arguments is a magic variable for functions containing an array of arguments for the function. So arguments[0] would be the first argument of the function the code is inside of. So essentially in your code you’re calling the same function twice. Hope that explains it…

Thank you for your answer, I will insist on learning, until learned:slight_smile:

Right, arguments[0] is the first argument.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.