Skip to content

Commit bb48d2e

Browse files
author
James Allardice
committed
Added article on JSHint freeze option
1 parent 698b81f commit bb48d2e

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

option-articles/freeze-jshint.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<!---
2+
{
3+
"titles": [
4+
"freeze"
5+
],
6+
"slugs": [
7+
"option-jshint-freeze"
8+
],
9+
"linters": [
10+
"jshint"
11+
],
12+
"author": "jallardice",
13+
"subject": "option"
14+
}
15+
-->
16+
17+
### What does this option do?
18+
19+
The JSHint `freeze` option is used to disallow the extension of native object
20+
prototypes. This is often viewed as bad practice and was a relatively common
21+
source of bugs in older JavaScript environments. In the following example we
22+
attempt to add a method to reverse strings to the native `String` object:
23+
24+
<!---
25+
{
26+
"linter": "jshint"
27+
}
28+
-->
29+
```javascript
30+
/*jshint freeze: true */
31+
String.prototype.reverse = function () {
32+
return this.split("").reverse().join("");
33+
};
34+
```
35+
36+
The option does not prevent extension of native prototypes via the
37+
`Object.defineProperty` method since it allows you to safely extend such objects
38+
with non-enumerable properties:
39+
40+
<!---
41+
{
42+
"linter": "jshint"
43+
}
44+
-->
45+
```javascript
46+
/*jshint freeze: true */
47+
Object.defineProperty(String.prototype, "reverse", {
48+
value: function () {
49+
return this.split("").reverse().join("");
50+
}
51+
});
52+
```
53+
54+
*Side note*: the implementation of string reversal above is naive because it
55+
fails to take into account the way characters are encoded internally in
56+
JavaScript. See [this Stack Overflow answer][reverse] for a great explanation.
57+
58+
### When should I use this option?
59+
60+
The use of the `freeze` JSHint option will cause an "[Extending prototype of
61+
native object: '{a}'][native]" error, where "{a}" is the native prototype in
62+
question, any time it encounters an assignment that matches the rules discussed
63+
above. It's generally considered bad practice to extend native prototypes
64+
because it makes it easier to introduce bugs related to property enumeration and
65+
shadowing. See the article discussing the "[Extending prototype of native
66+
object: '{a}'][native]" error message for more details.
67+
68+
Note that this is an *enforcing* option which means JSHint does not apply it by
69+
default. If you do not explicitly set this option to `true` JSHint will allow
70+
the extension of native prototypes anywhere in your code.
71+
72+
#### Recommendation
73+
74+
Set this option to `true` (disallows the extension of native prototypes).
75+
76+
[reverse]: http://stackoverflow.com/questions/958908/how-do-you-reverse-a-string-in-place-in-javascript/16776621#16776621
77+
[native]: /extending-prototype-of-native-object

0 commit comments

Comments
 (0)