Skip to content
This repository was archived by the owner on Aug 9, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions es5/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1178,18 +1178,29 @@
});
```

- Use a leading underscore `_` when naming private properties.
- Never use a leading underscore `_` when naming properties that are meant to
be used as private. This is a form of Hungarian notation (evil!) that
unnecessarily pollutes the code. JavaScript does not really support the
concept of variable privacy so there is no need to resort to this kind of
tricks to fake it.

```javascript
// bad
this.__firstName__ = 'Panda';
this.firstName_ = 'Panda';
this._firstName = 'Panda';

// good
this._firstName = 'Panda';
this.firstName = 'Panda';
```

- When saving a reference to `this` use `_this`.
- Avoid referencing `this` and opt for scope binding instead. You typically
end up needing it when handling events in jQuery and this technique
encourages you to decouple event listeners from their handlers, makes the
code simpler and much easier to unit test.

If you really need to reference the `this` scope, never use `_this` but
`context` instead.

```javascript
// bad
Expand All @@ -1208,13 +1219,19 @@
};
}

// good
// bad
function() {
var _this = this;
return function() {
console.log(_this);
};
}

// good
_.bindAll( this, 'handleClickEvent' );
function() {
$( target ).on( 'click', this.handleClickEvent );
}
```

- Name your functions. This is helpful for stack traces.
Expand Down