0

here is a simple example about 'this' keyword

var a = 10;
var obj = {
  a: 20
};

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

fn(); // 10
fn.call(obj); // 20

however, when I try to run it, the result shown as below.

undefined
20

why the first call return undefined? I don't get it. Can somebody help explain it to me? Many thanks

RH-st
  • 53
  • 6
  • If you copy-paste that into a console, it'll log `10`. If you're running this in some online Javascript interpreter, you're probably not actually running this code in global scope and hence will see a different behaviour. – deceze Aug 04 '20 at 07:12
  • @deceze it only displays `10` if strict mode is NOT activated right? – Ifaruki Aug 04 '20 at 07:49

0 Answers0