This correlation

1. Code output results

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

function doFoo() {
  foo();
}

var obj = {
  a: 1,
  doFoo: doFoo
};

var a = 2; 
obj.doFoo()
Copy the code

2. Code output results

var a = 10
var obj = {
  a: 20,
  say: () => {
    console.log(this.a)
  }
}
obj.say() 

var anotherObj = { a: 30 } 
obj.say.apply(anotherObj) 
Copy the code

3. Code output results

function a() {
  console.log(this);
}
a.call(null);
Copy the code

4. Code output results

var obj = { 
  name: 'cuggz', 
  fun: function(){ 
     console.log(this.name); 
  } 
} 
obj.fun()     // 
new obj.fun() // 
Copy the code

5. Code output results

var obj = {
  say: function() {
    var f1 = () =>  {
      console.log("1111", this);
    }
    f1();
  },
  pro: {
    getPro:() =>  {
       console.log(this);
    }
  }
}
var o = obj.say;
o();
obj.say();
obj.pro.getPro();
Copy the code

6. Code output results

var myObject = {
  foo: "bar",
  func: function() {
      var self = this;
      console.log(this.foo);  
      console.log(self.foo);  
      (function() {
          console.log(this.foo);  
          console.log(self.foo);  
      }());
  }
};
myObject.func();
Copy the code

7. Code output results

window.number = 2;
var obj = {
 number: 3,
 db1: (function(){
   console.log(this);
   this.number *= 4;
   return function(){
     console.log(this);
     this.number *= 5;
   }
 })()
}
var db1 = obj.db1;
db1();
obj.db1();
console.log(obj.number);     // 
console.log(window.number);  // 
Copy the code

8. Code output results

var length = 10; function fn() { console.log(this.length); } var obj = { length: 5, method: function(fn) { fn(); arguments[0](); }}; obj.method(fn, 1);Copy the code

9. Code output results

var a = 1;
function printA(){
  console.log(this.a);
}
var obj={
  a:2,
  foo:printA,
  bar:function(){
    printA();
  }
}

obj.foo(); // 
obj.bar(); // 
var foo = obj.foo;
foo(); // 
Copy the code

10. Code output results

var x = 3; var y = 4; var obj = { x: 1, y: 6, getX: function() { var x = 5; return function() { return this.x; } (); }, getY: function() { var y = 7; return this.y; } } console.log(obj.getX()) // console.log(obj.getY()) //Copy the code

11. Code output results

var a = 10; 
var obt = { 
  a: 20, 
  fn: function(){ 
    var a = 30; 
    console.log(this.a)
  } 
}
obt.fn();  // 
obt.fn.call(); // 
(obt.fn)(); // 
Copy the code

12. Code output results

function a(xx){
  this.x = xx;
  return this
};
var x = a(5);
var y = a(6);

console.log(x.x)  // 
console.log(y.x)  // 
Copy the code

13. Code output

function foo(something){
  this.a = something
}

var obj1 = {
  foo: foo
}

var obj2 = {}

obj1.foo(2); 
console.log(obj1.a); // 

obj1.foo.call(obj2, 3);
console.log(obj2.a); // 

var bar = new obj1.foo(4)
console.log(obj1.a); // 
console.log(bar.a); //
Copy the code

14. Code output results

function foo(something){
  this.a = something
}

var obj1 = {}

var bar = foo.bind(obj1);
bar(2);
console.log(obj1.a); 

var baz = new bar(3);
console.log(obj1.a); 
console.log(baz.a); 
Copy the code