-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_function.js
More file actions
53 lines (36 loc) · 762 Bytes
/
new_function.js
File metadata and controls
53 lines (36 loc) · 762 Bytes
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
alert = console.log
{
function User() {
alert(new.target);
}
// without "new":
User(); // undefined
// with "new":
new User(); // function User { ... }
}
{
function User(name) {
if (!new.target)
{ // if you run me without new
return new User(name); // ...I will add new for you
}
this.name = name;
}
let john = User("John"); // redirects call to new User
alert(john.name); // John
}
{
function BigUser() {
this.name = "John";
return { name: "Godzilla" }; // <-- returns an object
}
alert( new BigUser().name ); // Godzilla, got that object ^^
}
{
function SmallUser() {
this.name = "John";
return; // finishes the execution, returns this
// ...
}
alert( new SmallUser().name ); // John
}