-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype_5.js
More file actions
36 lines (21 loc) · 1018 Bytes
/
prototype_5.js
File metadata and controls
36 lines (21 loc) · 1018 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
function Person(name, age){
this.name = name;
this.age = age;
}
const p1 = Person("Nonna", 33);
console.log(p1.name);
/*
This code defines a constructor function `Person` that sets:
- `name` property from the parameter
- `age` property from the parameter
Then, it calls `Person("Nonna", 33)` without the `new` keyword and assigns the result to `p1`.
Important points:
1. When calling a constructor function without `new`, the function is executed as a normal function.
2. Inside `Person`, `this` refers to the global object (in non-strict mode), so
`this.name` and `this.age` set global variables, NOT properties on a new object.
3. The function `Person` does NOT return anything explicitly, so `p1` is `undefined`.
4. Therefore, `p1.name` throws a "TypeError" because `p1` is `undefined` and does not have properties.
To fix this, call the constructor with `new`:
`const p1 = new Person("Nonna", 33)`
Output: TypeError: Cannot read property 'name' of undefined
*/