Friday, December 14, 2018

Javascript - this, bind, call

// this , bind, call

var msg = "global";

function f(x){
  console.log(this.msg + "," + x);
}

var obj1 = {
  msg: "obj1",
  f: f
}

var obj2 = {
  msg: "obj2"
}

f(10);
obj1.f(10);
f.call(obj2, 10);

// f.call(window, 10);
// f.call(obj1, 10);
// f.call(obj2, 10);

// f.bind(obj1)(10)
// f.bind(obj2)(10)
// f.bind(window)(10)

No comments:

Post a Comment

Followers

Blog Archive