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
<!DOCTYPE html> | |
<!-- saved from url=(0051)http://www.jspatterns.com/book/7/observer-game.html --> | |
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> | |
<title>Keypress challenge</title> | |
<style> | |
#results {text-align: center; font-size: 36px;} | |
strong {color: blue;} | |
</style> | |
</head> | |
<body cz-shortcut-listen="true"> |
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
<!DOCTYPE html> | |
<!-- saved from url=(0046)http://www.jspatterns.com/book/7/observer.html --> | |
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> | |
<title>Observer</title> | |
</head> | |
<body cz-shortcut-listen="true"> | |
<script> | |
"use strict"; |
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
<!DOCTYPE html> | |
<!-- saved from url=(0046)http://www.jspatterns.com/book/7/mediator.html --> | |
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> | |
<title>Mediator pattern</title> | |
<style> | |
#results {text-align: center; font-size: 100px;} | |
strong {color: blue;} | |
</style> | |
</head> | |
<body cz-shortcut-listen="true"> |
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | |
<!-- saved from url=(0043)http://www.jspatterns.com/book/7/proxy.html --> | |
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<title>Dave Matthews vids</title> | |
<style> | |
img {float: left; padding-right: 7px;} | |
div {padding: 7px; border: 1px solid #ccc; background: #eee; width: 400px;} | |
h2 {margin: 0;} | |
span {text-decoration: underline; cursor: pointer;} |
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
/* Title: Strategy | |
Description: allows one of a family of algorithms to be selected on-the-fly at runtime | |
*/ | |
// 用法 | |
var data = { | |
first_name: "Super", | |
last_name: "Man", | |
age: "unknown", |
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
// 有個Sale物件代表一筆銷售,呼叫`Sale.getPrice()`取得價格,額外的功能可以用`Sale.decorate()`附加至物件上 | |
// 用法 | |
var sale = new Sale(100); // 售價是100 | |
sale.decorate('fedtax'); // 加上聯邦稅//"$112.88元 | |
sale sale.decorate('quebec'); // 再加上魅北克省稅 | |
sale.decorate('money'); // 格式化成金額 | |
sale.getPrice(); // "$112.88" | |
sale = new Sale(100); // 售價是100元 | |
sale.decorate('fedtax'); // 加上聯邦稅 |
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 element; | |
while (element = agg.next()) { | |
// do something with the element | |
console.log(element); | |
} | |
// use hasNext() | |
while (agg.hasNext()) { | |
// do something with the next element... |
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 corolla = CarMaker.factory('Compact'); | |
var solstice = CarMaker.factory('Convertible'); | |
var cherokee = CarMaker.factory('SUV'); | |
corolla.drive(); // "Vroom , I have 4 doors" | |
soIstice.drive(); // "Vroom , I have 2 doors" | |
cherokee.drive(); // "Vroom , I have 17 doors" | |
// 父建構式 | |
function CarMaker(){} |
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
// 儲存在靜態屬性中的實體 | |
function Universe() { | |
// do we have an existing instance? | |
if (typeof Universe.instance === 'object') { | |
return Universe.instance; | |
} | |
// proceed as normal | |
this.start_time = 0; |