Skip to content

Commit

Permalink
new agnostic tip
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 29, 2016
1 parent 155db38 commit 1dc50f0
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ Also be careful about choose betweetn `Object` or `Array` for this purpose:
- If you need a incremental index, use `Array`.
- In other case use `Object`.

Under `ES2015`, consider use `Map` or `Set` combined with `Symbol`.

Althought is more readable, not in all scenarios use a lookup table is better: The cost of create the lookup table could be higher tan use a set of if/else statements. So, it depends about your code running time:

- If you are going to be running short time (maybe just one execution, like building a CLI tool) use if/else.
Expand Down Expand Up @@ -453,6 +455,30 @@ if (_.isError(result)) {

### Function

#### Make Your Constructors new-Agnostic

Even your function is called or not using `new` keyword, you can force to have the same behavior in both cases:

```js
function User(name, passwordHash) {
if (!(this instanceof User)) return new User(name, passwordHash) // magic line!

this.name = name
this.passwordHash = passwordHash
}
```

Now, the behavior is the expected in both cases:

```
var x = User("baravelli", "d8b74df393528d51cd19980ae0aa028e")
var y = new User("baravelli","d8b74df393528d51cd19980ae0aa028e")
x instanceof User // true
y instanceof User // true
```


#### Avoid .bind, is slower.

In general terms, `.bind` the context with a `Function` generate need a considerable effort.
Expand Down

0 comments on commit 1dc50f0

Please sign in to comment.