Skip to content

Commit

Permalink
add all files
Browse files Browse the repository at this point in the history
  • Loading branch information
arthinking committed Oct 21, 2012
1 parent bf6cc44 commit 943d57d
Show file tree
Hide file tree
Showing 105 changed files with 6,395 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Source Code</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
<nature>com.aptana.projects.webnature</nature>
</natures>
</projectDescription>
89 changes: 89 additions & 0 deletions Chapter01/1.01 - The flexibility of JavaScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* below is five method to do the same thing */
/* a. Start and stop animations using functions. */

function startAnimation() {
...
}

function stopAnimation() {
...
}



/* b. Anim class. */

var Anim = function() {
...
};
Anim.prototype.start = function() {
...
};
Anim.prototype.stop = function() {
...
};

/* Usage. */

var myAnim = new Anim();
myAnim.start();
...
myAnim.stop();



/* c. Anim class, with a slightly different syntax for declaring methods. */

var Anim = function() {
...
};
Anim.prototype = {
start: function() {
...
},
stop: function() {
...
}
};



/* d. Add a method to the Function class that can be used to declare methods. */

Function.prototype.method = function(name, fn) {
this.prototype[name] = fn;
};

/* Anim class, with methods created using a convenience method. */

var Anim = function() {
...
};
Anim.method('start', function() {
...
});
Anim.method('stop', function() {
...
});



/* e. This version allows the calls to be chained. */

Function.prototype.method = function(name, fn) {
this.prototype[name] = fn;
return this;
};

/* Anim class, with methods created using a convenience method and chaining. */

var Anim = function() {
...
};
Anim.
method('start', function() {
...
}).
method('stop', function() {
...
});
39 changes: 39 additions & 0 deletions Chapter01/1.02 - Functions as first-class objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* An anonymous function, executed immediately. */

(function() {
var foo = 10;
var bar = 2;
alert(foo * bar);
})();


/* An anonymous function with arguments. */

(function(foo, bar) {
alert(foo * bar);
})(10, 2);


/* An anonymous function that returns a value. */

var baz = (function(foo, bar) {
return foo * bar;
})(10, 2);

// baz will equal 20.


/* An anonymous function used as a closure. */

var baz;

(function() {
var foo = 10;
var bar = 2;
baz = function() {
return foo * bar;
};
})();

baz(); // baz can access foo and bar, even though is it executed outside of the
// anonymous function.
38 changes: 38 additions & 0 deletions Chapter01/1.03 - The mutability of objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function displayError(message) {
displayError.numTimesExecuted++;
alert(message);
};
displayError.numTimesExecuted = 0;


/* Class Person. */

function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype = {
getName: function() {
return this.name;
},
getAge: function() {
return this.age;
}
}

/* Instantiate the class. */

var alice = new Person('Alice', 93);
var bill = new Person('Bill', 30);

/* Modify the class. */

Person.prototype.getGreeting = function() {
return 'Hi ' + this.getName() + '!';
};

/* Modify a specific instance. */

alice.displayGreeting = function() {
alert(this.getGreeting());
}
35 changes: 35 additions & 0 deletions Chapter02/2.01 - Describing interfaces with comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
interface Composite {
function add(child);
function remove(child);
function getChild(index);
}
interface FormItem {
function save();
}
*/

var CompositeForm = function(id, method, action) { // implements Composite, FormItem
...
};

// Implement the Composite interface.

CompositeForm.prototype.add = function(child) {
...
};
CompositeForm.prototype.remove = function(child) {
...
};
CompositeForm.prototype.getChild = function(index) {
...
};

// Implement the FormItem interface.

CompositeForm.prototype.save = function() {
...
};
49 changes: 49 additions & 0 deletions Chapter02/2.02 - Emulating interfaces with attribute checking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
interface Composite {
function add(child);
function remove(child);
function getChild(index);
}
interface FormItem {
function save();
}
*/

var CompositeForm = function(id, method, action) {
this.implementsInterfaces = ['Composite', 'FormItem'];
...
};

...

function addForm(formInstance) {
if(!implements(formInstance, 'Composite', 'FormItem')) {
throw new Error("Object does not implement a required interface.");
}
...
}

// The implements function, which checks to see if an object declares that it
// implements the required interfaces.

function implements(object) {
for(var i = 1; i < arguments.length; i++) { // Looping through all arguments
// after the first one.
var interfaceName = arguments[i];
var interfaceFound = false;
for(var j = 0; j < object.implementsInterfaces.length; j++) {
if(object.implementsInterfaces[j] == interfaceName) {
interfaceFound = true;
break;
}
}

if(!interfaceFound) {
return false; // An interface was not found.
}
}
return true; // All interfaces were found.
}
18 changes: 18 additions & 0 deletions Chapter02/2.03 - Emulating interfaces with duck typing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Interfaces.

var Composite = new Interface('Composite', ['add', 'remove', 'getChild']);
var FormItem = new Interface('FormItem', ['save']);

// CompositeForm class

var CompositeForm = function(id, method, action) {
...
};

...

function addForm(formInstance) {
ensureImplements(formInstance, Composite, FormItem);
// This function will throw an error if a required method is not implemented.
...
}
20 changes: 20 additions & 0 deletions Chapter02/2.04 - The interface implementation for this book.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Interfaces.

var Composite = new Interface('Composite', ['add', 'remove', 'getChild']);
var FormItem = new Interface('FormItem', ['save']);

// CompositeForm class

var CompositeForm = function(id, method, action) { // implements Composite, FormItem
...
};

...

function addForm(formInstance) {
Interface.ensureImplements(formInstance, Composite, FormItem);
// This function will throw an error if a required method is not implemented,
// halting execution of the function.
// All code beneath this line will be executed only if the checks pass.
...
}
44 changes: 44 additions & 0 deletions Chapter02/2.05 - The Interface class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Constructor.

var Interface = function(name, methods) {
if(arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length
+ "arguments, but expected exactly 2.");
}

this.name = name;
this.methods = [];
for(var i = 0, len = methods.length; i < len; i++) {
if(typeof methods[i] !== 'string') {
throw new Error("Interface constructor expects method names to be "
+ "passed in as a string.");
}
this.methods.push(methods[i]);
}
};

// Static class method.

Interface.ensureImplements = function(object) {
if(arguments.length < 2) {
throw new Error("Function Interface.ensureImplements called with " +
arguments.length + "arguments, but expected at least 2.");
}

for(var i = 1, len = arguments.length; i < len; i++) {
var interface = arguments[i];
if(interface.constructor !== Interface) {
throw new Error("Function Interface.ensureImplements expects arguments "
+ "two and above to be instances of Interface.");
}

for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
var method = interface.methods[j];
if(!object[method] || typeof object[method] !== 'function') {
throw new Error("Function Interface.ensureImplements: object "
+ "does not implement the " + interface.name
+ " interface. Method " + method + " was not found.");
}
}
}
};
9 changes: 9 additions & 0 deletions Chapter02/2.06 - When to use the Interface class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var DynamicMap = new Interface('DynamicMap', ['centerOnPoint', 'zoom', 'draw']);

function displayRoute(mapInstance) {
Interface.ensureImplements(mapInstace, DynamicMap);
mapInstance.centerOnPoint(12, 34);
mapInstance.zoom(5);
mapInstance.draw();
...
}
Loading

0 comments on commit 943d57d

Please sign in to comment.