Last active
March 13, 2019 21:05
-
-
Save elijahmanor/5328936 to your computer and use it in GitHub Desktop.
Angry Birds of JavaScript: Big Brother Bird - Design Patterns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* jquery-win8-deferred - jQuery $.when that understands WinJS.promise | |
* version: 0.1 | |
* author: appendTo, LLC | |
* copyright: 2012 | |
* license: MIT (http://www.opensource.org/licenses/mit-license) | |
* date: Thu, 01 Nov 2012 07:38:13 GMT | |
*/ | |
(function () { | |
var $when = $.when; | |
$.when = function () { | |
var args = Array.prototype.slice.call(arguments); | |
args = $.map(args, function (arg) { | |
if (arg instanceof WinJS.Promise) { | |
arg = $.Deferred(function (dfd) { | |
arg.then( | |
function complete() { | |
dfd.resolveWith(this, arguments); | |
}, function error() { | |
dfd.rejectWith(this, arguments); | |
}, function progress() { | |
dfd.notifyWith(this, arguments); | |
} | |
); | |
}).promise(); | |
} | |
return arg; | |
}); | |
return $when.apply(this, args); | |
}; | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Not Bridged | |
var getUrl = function() { | |
var url = $( this ).attr( "href" ); | |
$.ajax({ | |
url: url, | |
success: function( data ) { | |
console.log( data ); | |
} | |
}); | |
}; | |
$( "a.ajax" ).on( "click", getUrl ); | |
// Bridged | |
var getUrl = function( url, callback ) { | |
$.ajax({ | |
url: url, | |
success: function( data ) { | |
if ( callback ) { callback( data ); } | |
} | |
}); | |
}; | |
var getUrlBridge = function() { | |
var url = $( this ).attr( "href" ); | |
getUrl( url, function( data ) { | |
console.log( data ); | |
}); | |
} | |
$( "a.ajax" ).on( "click", getUrlBridge ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bird = { | |
catapult: function() { | |
console.log( "Yippeeeeee!" ); | |
return this; | |
}, | |
destroy: function() { | |
console.log( "That'll teach you... you dirty pig!" ); | |
return this; | |
} | |
}; | |
bird.catapult().destroy(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// IIFE | |
var yellowBird = (function() { | |
var superSecret = { | |
power: "Speed" | |
}; | |
return { | |
type: "Red", | |
mood: "Angry", | |
goal: "Vengence" | |
} | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Facade | |
var addEvent = function( element, type, eventHandler ) { | |
if ( element.addEventListener ) { | |
element.addEventListener( type, eventHandler, false ); | |
} else if ( elemement.attachEvent ) { | |
element.attachEvent( "on" + type, eventHandler ); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Bird = function() {}; | |
Bird.factory = function( type ) { | |
var bird; | |
if ( typeof Bird[ type ] === "function" ) { | |
bird = new Bird[ type ](); | |
} | |
return bird; | |
}; | |
Bird.Red = function() {}; | |
Bird.Blue = function() {}; | |
var redBird = Bird.factory( "Red" ); | |
var blueBird = Bird.factory( "Blue" ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var attackFsm = new machina.Fsm({ | |
initialState: "idle", | |
states : { | |
"idle" : { | |
_onEnter: function() { | |
this.handle( "Zzzzzz" ); | |
}, | |
"bird.launch" : function( data ) { | |
console.log( "Weeeeee at " + data.angle + " degrees!" ); | |
this.transition( "attacking" ); | |
} | |
}, | |
"attacking" : { | |
_onEnter: function() { | |
console.log( "Yay, hear me tweet!" ); | |
}, | |
"pig.destroyed" : function() { | |
this.transition( "victorious" ); | |
}, | |
"pig.alive" : function() { | |
this.transition( "defeated" ); | |
} | |
}, | |
"victorious": { | |
_onEnter: function() { | |
console.log( "Yay, we are victorious!" ); | |
}, | |
"game.restart": function() { | |
this.transition( "idle" ); | |
}, | |
"game.next": function() { | |
// Goto next level | |
this.transition( "idle" ); | |
} | |
}, | |
"defeated": { | |
_onEnter: function() { | |
console.log( "You may have won this time, but I'll be back!" ); | |
}, | |
"gmae.restart": function() { | |
this.transition( "idle" ); | |
} | |
} | |
} | |
}); | |
attackFsm.handle( "bird.launch", { angle: 45 } ); | |
attackFsm.handle( "pig.destroyed" ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var channel = postal.channel( "game" ); | |
channel.subscribe( "bird.attack", function( data ) { | |
console.log( "Geronimo!" ); | |
}); | |
channel.subscribe( "pig.collide", function( impact ) { | |
if ( impact > 100 ) { | |
console.log( "AHHHHHHH!" ); | |
} | |
}); | |
channel.publish( "bird.attack", { angle: 45 } ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bird = { | |
name: "Red Bird", | |
power: "", | |
getName: function() { | |
return this.name; | |
}, | |
catapult: function() { | |
return this.name + " is catapulting with " + this.power; | |
} | |
}; | |
var yellowBird = Object.create( bird ); | |
yellowBird.name = "Yellow Bird"; | |
yellowBird.power = "Speed"; | |
console.log( yellowBird.catapult() ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Bird = function( name, power ) { | |
this.name = name + " Bird"; | |
this.power = power || ""; | |
}; | |
Bird.prototype.getName = function() { | |
return this.name; | |
}; | |
Bird.prototype.catapult = function() { | |
return this.getName() + " is catapulting with " + this.power; | |
}; | |
var YellowBird = function() { | |
this.constructor.apply( this, arguments ); | |
}; | |
YellowBird.prototype = new Bird(); | |
var yellowBird = new YellowBird( "Yellow", "Speed" ); | |
yellowBird.getName = function() { | |
return "Super Awesome " + this.name; | |
}; | |
console.log( yellowBird.catapult() ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bird = { | |
type: "Red", | |
fly: function() { | |
console.log( "Weeeee!" ); | |
}, | |
destroy: function() { | |
console.log( "Hasta la vista, baby!" ); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @elijahmanor - small typo in factory.js:
var redBird = Bird.factory( "Red" );
var blueBird = Bird.factor( "Blue" );
missing the "y" on "Blue"