Skip to content
This repository
branch: master
February 01, 2012
file 44 lines (35 sloc) 1.144 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Command
Description: creates objects which encapsulate actions and parameters
*/

(function () {

var CarManager = {

/* request information */
requestInfo:function (model, id) {
return 'The purchase info for ' + model + ' with ID ' + id + ' is being processed...';
},

/* purchase the car */
buyVehicle:function (model, id) {
return 'You have successfully purchased Item ' + id + ', a ' + model + '.';
}

};

CarManager.execute = function (commad) {
return CarManager[commad.request](commad.model, commad.carID);
};

var actionA = CarManager.execute({request:'requestInfo', model:'Ford Mondeo', carID:'543434'});
console.log(actionA);
var actionB = CarManager.execute({request:'buyVehicle', model:'Ford Mondeo', carID:'543434'});
console.log(actionB);

})();

// reference
// http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#commandpatternjavascript
</script>
</body>
</html>
Something went wrong with that request. Please try again.