Sometimes you'll have objects that manage state along with event handling. This happens frequently in MVC apps. Let's start with a messy example:
var Launcher = function(rocket) {
this.rocket = rocket
}
Launcher.prototype.isReady = function() {
| var flattenObject = function(ob) { | |
| var toReturn = {}; | |
| for (var i in ob) { | |
| if (!ob.hasOwnProperty(i)) continue; | |
| if ((typeof ob[i]) == 'object') { | |
| var flatObject = flattenObject(ob[i]); | |
| for (var x in flatObject) { | |
| if (!flatObject.hasOwnProperty(x)) continue; |
| :+1: | |
| :-1: | |
| :airplane: | |
| :art: | |
| :bear: | |
| :beer: | |
| :bike: | |
| :bomb: | |
| :book: | |
| :bulb: |
| YUI().use("singleton-class", function (Y) { | |
| var instance1 = new Y.SingletonClass(), | |
| instance2 = new Y.SingletonClass(); | |
| Y.log(instance1 === instance2); // true! | |
| window.instance1 = instance1; | |
| }); | |
| YUI().use("singleton-class", function (Y) { |
| function maybe(value) { | |
| var obj = null; | |
| function isEmpty() { return value === undefined || value === null } | |
| function nonEmpty() { return !isEmpty() } | |
| obj = { | |
| map: function (f) { return isEmpty() ? obj : maybe(f(value)) }, | |
| getOrElse: function (n) { return isEmpty() ? n : value }, | |
| isEmpty: isEmpty, | |
| nonEmpty: nonEmpty | |
| } |
| // Lack of tail call optimization in JS | |
| var sum = function(x, y) { | |
| return y > 0 ? sum(x + 1, y - 1) : | |
| y < 0 ? sum(x - 1, y + 1) : | |
| x | |
| } | |
| sum(20, 100000) // => RangeError: Maximum call stack size exceeded | |
| // Using workaround |
| YUI.add('multi', function (Y) { | |
| /** | |
| Loads different versions of YUI in sandboxed iframes and makes them available | |
| for use in the parent frame by exposing them as properties on a `Y.Multi` | |
| instance. | |
| This is primarily intended to be used for testing. You probably shouldn't use it | |
| for anything real. |
| #!/bin/bash | |
| ## ================================== | |
| ## Servers | |
| ## ================================== | |
| # A server with a non-standard SSH port, using passwordless RSA key authentication | |
| alias server1='ssh -p 2200 user@server-domain.com' | |
| # A server that uses a certficate for authentication |
git update-index --assume-unchanged dir-im-removing/
git update-index --assume-unchanged config/database.yml
git update-index --skip-worktree
git rebase -i <earliercommit>
git commit --amend --author="Author Name <email@address.com>"