• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16interface Author {
17  firstName: string;
18  lastName: string;
19  anotherName: Function;
20  asyncName: Function;
21}
22
23class C {
24  firstName: string;
25  lastName: string;
26  constructor(firstName: string, lastName: string) {
27    this.firstName = firstName;
28    this.lastName = lastName;
29    this.fullName = this.fullName.bind(this);
30    this.asyncFullName = this.asyncFullName.bind(this);
31   }
32
33   author(): Author {
34       let author: Author = {
35         firstName: this.firstName,
36         lastName: this.lastName,
37         anotherName: this.fullName.bind(this),
38         asyncName: this.asyncFullName.bind(this)
39       }
40       return author
41   }
42
43  fullName(): string {
44    return this.firstName + " " + this.lastName;
45  }
46
47  async asyncFullName(): Promise<string> {
48    return Promise<string>(fullName());
49  }
50
51  sayHello(greet: string) {
52    console.log(greet, this.fullName());
53  }
54
55  async asyncSayHello(greet: string) {
56    const afn = await this.asyncFullName();
57    console.log(greet, afn);
58  }
59}
60
61async function asyncFoo(): Promise<void> {
62  const person1 = new C("Arthur", "Clarke");
63  const person2 = new C("Ray", "Bradbury");
64
65  await person1.asyncFullName();
66  const afn = await person1.asyncFullName();
67
68  const afn2 = await person1.asyncFullName.apply(person2);
69
70  const f = person2.asyncFullName.bind(person1);
71  await f();
72
73  await asyncBar(person1);
74
75  const ash1: Function = person1.asyncSayHello.bind(person1, "Hello");
76  await ash1()
77
78  const ash2: Function = person1.asyncSayHello.bind(person1);
79  await ash2("Hello")
80
81  await person1.asyncSayHello.apply(person2, "Hello");
82  await person1.asyncSayHello.call(person2, "Hello")
83}
84
85async function asyncBar(c: C): Promise<string> {
86  const person = new C("Stanislaw", "Lem");
87  return await c.asyncFullName.call(person);
88}
89
90function foo(): void {
91  const person1 = new C("Arthur", "Clarke");
92  const person2 = new C("Ray", "Bradbury");
93
94  const fullName = person1.fullName.apply(person2);
95
96  const f = person2.fullName.bind(person1);
97  f();
98
99  bar(person1);
100
101  const sh1: Function = person1.sayHello.bind(person1, "Hello");
102  sh1()
103
104  const sh2: Function = person1.sayHello.bind(person1);
105  sh2("Hello")
106
107  person1.sayHello.apply(person2, "Hello");
108  person1.sayHello.call(person2, "Hello")
109}
110
111function bar(c: C): string {
112  const person = new C("Stanislaw", "Lem");
113  return c.fullName.call(person);
114}
115
116const person = {
117  fn: "Ben",
118  f1: function () {
119    return this.fn; // here `this` is the current obj
120  },
121  f2: function (): string {
122    return this.fn; // here `this` is the current obj
123  },
124  f3: () => {
125    return this.fo; // here `this` is `globalThis`
126  },
127  f4: (): string => {
128    return this.fo; // here `this` is `globalThis`
129  },
130};
131
132const person1 = {
133  fn: "Mary",
134};
135
136console.log(person.f1.apply(person1));
137console.log(person.f2.apply(person1));
138console.log(person.f3.apply(person1));
139console.log(person.f4.apply(person1));
140
141foo.apply(undefined);
142