# 字节面试题

// 1.
var name = "window";
var bar = { name: "bar" };

var foo = {
  name: "foo",
  say: function() {
    // console.log(this);
    console.log(this.name);
  },
  say1: () => console.log(this.name),
  say2: function() {
    return () => {
      console.log(this.name);
    };
  },
};

foo.say(); // foo
foo.say.call(bar); // bar
foo.say1(); // window
foo.say1.call(bar); // window
foo.say2()(); // foo
foo.say2.call(bar)(); // bar
foo.say2().call(bar); // foo

class Nww {
  a() {
    console.log(this);
  }
  b = () => {
    console.log(this);
  };
}

const n = new Nww();
console.log(n.a(), n.b());

// 2.
inner = "window";

function say() {
  console.log(inner);
  console.log(this.inner);
}

var obj1 = (function() {
  var inner = "1-1";
  return {
    inner: "1-2",
    say: function() {
      console.log(inner);
      console.log(this.inner);
    },
  };
})();

var obj2 = (function() {
  var inner = "2-1";
  return {
    inner: "2-2",
    say: function() {
      console.log(inner);
      console.log(this.inner);
    },
  };
})();

say(); // window window
obj1.say(); // 1-1 1-2
obj2.say(); // 2-1 2-2
obj1.say = say;
obj1.say(); // window 1-2
obj1.say = obj2.say;
obj1.say(); // 2-1 1-2

// person1.show1(); person1
// person1.show1.call(person2); person2

// person1.show2(); window
// person1.show2.call(person2); window

// person1.show3()(); window
// person1.show3().call(person2); person2
// person1.show3.call(person2)(); window

// person1.show4()(); person1 person1

// person1.show4().call(person2); person1 person1
// person1.show4.call(person2)(); person2 person2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Last Updated: 6/27/2023, 7:40:45 PM