1

If null or nothing is passed in call(), would it be considered as implicit binding? What should be the output of the following code? Isn't it should be 2? I am getting 'undefined'.

function foo() {
console.log( this.a );
}

var a = 2;

foo.call( null ); // 2
1
  • 1
    From mdn; "If the method is a function in non-strict mode, null and undefined will be replaced with the global object" Commented Apr 6, 2021 at 18:46

1 Answer 1

4

It is an implicit binding, you can check that with console.log(this);. It prints the global object. So, when run in my browser, your code prints 2.

However, such var a = 2; statement works differently in Node.js: it does not create a property of the global object. So this.a tries to access a non-existing member which results in undefined.

Sign up to request clarification or add additional context in comments.

1 Comment

Additional info: the interactive Node console returns 2 as well (non-strict code) as it creates a global object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.