-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype_7.js
More file actions
33 lines (20 loc) · 1 KB
/
prototype_7.js
File metadata and controls
33 lines (20 loc) · 1 KB
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
"use strict";
function foo() {
}
foo.name = "bar";
console.log(foo.name);
/*
This code demonstrates a common mistake when trying to assign a value to a function's `name` property.
1. The "use strict" directive enables strict mode, which enforces stricter parsing and error handling.
2. A function `foo` is declared.
3. The code attempts to assign `bar` to `foo.name` without quotes.
- `bar` here is interpreted as an undefined variable (not a string).
4. Because `bar` is not defined anywhere, this causes a ReferenceError.
5. Additionally, the `name` property of functions is read-only and non-writable,
so even if you try `foo.name = "bar"`, in strict mode it will not change the function's name,
and may throw an error or silently fail depending on the environment.
To correctly check or set the name, you should:
- Use `foo.name` (read-only) to get the function's name.
- You cannot directly change `foo.name` by assignment.
Output: ReferenceError: bar is not defined
*/