Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
travisjeffery committed Oct 5, 2019
1 parent 1b578a3 commit 5917da5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ When you generate a migration, by following Rails' migration name conventions, R
end
end

You should always check the generated migration though, it's not perfect. The migration generated by this command should remove the index as well.
You should always check the generated migration though, it's not perfect. The migration generated by this command should remove the index, along with the column.

rails g migration remove_body_from_people body:string:index

Expand Down
63 changes: 23 additions & 40 deletions src/b/2012-03-20-javascript-conditional-assignment.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,26 @@ comments: false
collection: javascript
---

A common idiom with in programming is conditional assignment, usually to
set an undefined variable. Ruby has a nice operator, `||=` for this purpose,
JavaScript doesn't have such an operator. So often variables that are used
across multiple files, like namespaces, have to be repeatedly conditionally
assigned. By not doing so you may try to use the variable before it's been
declared and have a `ReferenceError` thrown.

There
[are](http://stackoverflow.com/questions/881515/javascript-namespace-declaration)
[lots](http://stackoverflow.com/questions/4401323/javascript-best-practice-define-variable-namespace-check-is-not-already-define)
[of](http://blog.arc90.com/2008/06/06/an-easy-way-to-implement-namespaces-in-javascript/)
[ways](http://stackoverflow.com/questions/7639268/nicer-way-of-checking-javascript-namespace) to conditionally assign in JavaScript.
This is the way I learned from Jeremy Ashkenas, and the way I like to do it:

``` js
var Namespace;
Namespace || (Namepace = {});
```

This can also be used for arguments and local variables,

``` js
Namespace.Model = {};

_.extend(Namespace.Model, {
set: function(attrs, options) {
options || (options = {});

if (options.logger) {
return options.logger.log("Hello. Yes, This is log.")
}
}
});

Namespace.Model.set({});
# => ...Nothing/no ReferenceError!

Namespace.Model.set({}, {logger: console})
# => "Hello. Yes, This is log."
```
Ruby has a conditional assignment operator: `||=`.
JavaScript doesn't have such an operator. Common use cases for conditional assignment that JavaScript developers use are lazily defining a namespace and defaulting function arguments.

Since JavaScript has no built in conditional assignment operator, the language leaves conditional assignment as a stylistic choice for JavaScript developers. The following is the style I like, which I learned from Jeremy Ashkenas.

- Defining a namespace:

var Namespace;
Namespace || (Namepace = {});

- Function arguments:

Namespace.Model = {};

_.extend(Namespace.Model, {
set: function(attrs, options) {
options || (options = {});

if (options.logger) {
return options.logger.log("Hello. Yes, This is log.")
}
}
});

0 comments on commit 5917da5

Please sign in to comment.