Skip to content

Commit

Permalink
Added first edition
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas C. Zakas committed Dec 7, 2009
1 parent 91f15cf commit c66a65a
Show file tree
Hide file tree
Showing 315 changed files with 12,443 additions and 0 deletions.
1 change: 1 addition & 0 deletions edition1/Windows XP Service Pack 2 README.txt
@@ -0,0 +1 @@
If you are using Internet Explorer on Windows XP Service Pack 2, there is a security restriction disallowing scripts to run in files opened on your local machine. When you first try to run the examples, you will most likely get a message saying, "To help protect your security, Internet Explorer has restricted this file from showing active content that could access your computer." To see the example, you must click on the message and select "Allow Blocked Content".
28 changes: 28 additions & 0 deletions edition1/ch03/ClassicExample.htm
@@ -0,0 +1,28 @@
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
function createCar(color, doors, mpg) {
var tempcar = new Object;
tempcar.color = color;
tempcar.doors = doors;
tempcar.mpg = mpg;
tempcar.showColor = function () {
alert(this.color)
};

return tempcar;
}

var car1 = createCar("red", 4, 23);
var car2 = createCar("blue", 3, 25);
car1.showColor(); //outputs "red"
car2.showColor(); //outputs "blue"


</script>

</body>
</html>
25 changes: 25 additions & 0 deletions edition1/ch03/ConstructorExample.htm
@@ -0,0 +1,25 @@
<html>
<head>
<title>Constructor Example</title>
</head>
<body>
<script type="text/javascript">
function Car(sColor, iDoors, iMpg) {
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
this.showColor = function () {
alert(this.color)
};
}

var oCar1 = new Car("red", 4, 23);
var oCar2 = new Car("blue", 3, 25);
oCar1.showColor(); //outputs "red"
oCar2.showColor(); //outputs "blue"


</script>

</body>
</html>
29 changes: 29 additions & 0 deletions edition1/ch03/ConstructorPrototypeHybridExample.htm
@@ -0,0 +1,29 @@
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
function Car(sColor, iDoors, iMpg) {
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
this.drivers = new Array("Mike", "Sue");
}

Car.prototype.showColor = function () {
alert(this.color);
};

var oCar1 = new Car("red", 4, 23);
var oCar2 = new Car("blue", 3, 25);

oCar1.drivers.push("Matt");

alert(oCar1.drivers); //outputs "Mike,Sue,Matt"
alert(oCar2.drivers); //outputs "Mike,Sue"

</script>

</body>
</html>
35 changes: 35 additions & 0 deletions edition1/ch03/DynamicPrototypeExample.htm
@@ -0,0 +1,35 @@
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
function Car(sColor, iDoors, iMpg) {
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
this.drivers = new Array("Mike", "Sue");

if (typeof Car._initialized == "undefined") {

Car.prototype.showColor = function () {
alert(this.color);
};

Car._initialized = true;
}
}


var oCar1 = new Car("red", 4, 23);
var oCar2 = new Car("blue", 3, 25);

oCar1.drivers.push("Matt");

alert(oCar1.drivers); //outputs "Mike,Sue,Matt"
alert(oCar2.drivers); //outputs "Mike,Sue"

</script>

</body>
</html>
28 changes: 28 additions & 0 deletions edition1/ch03/FactoryExample.htm
@@ -0,0 +1,28 @@
<html>
<head>
<title>Factory Example</title>
</head>
<body>
<script type="text/javascript">
function createCar(sColor, iDoors, iMpg) {
var oTempCar = new Object;
oTempCar.color = sColor;
oTempCar.doors = iDoors;
oTempCar.mpg = iMpg;
oTempCar.showColor = function () {
alert(this.color)
};

return oTempCar;
}

var oCar1 = createCar("red", 4, 23);
var oCar2 = createCar("blue", 3, 25);
oCar1.showColor(); //outputs "red"
oCar2.showColor(); //outputs "blue"


</script>

</body>
</html>
30 changes: 30 additions & 0 deletions edition1/ch03/PrototypeExample.htm
@@ -0,0 +1,30 @@
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
function Car() {
}

Car.prototype.color = "red";
Car.prototype.doors = 4;
Car.prototype.mpg = 23;
Car.prototype.drivers = new Array("Mike", "Sue");
Car.prototype.showColor = function () {
alert(this.color);
};

var oCar1 = new Car();
var oCar2 = new Car();

oCar1.drivers.push("Matt");

alert(oCar1.drivers); //outputs "Mike,Sue,Matt"
alert(oCar2.drivers); //outputs "Mike,Sue,Matt"


</script>

</body>
</html>
22 changes: 22 additions & 0 deletions edition1/ch03/RandomExample.htm
@@ -0,0 +1,22 @@
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
function selectFrom(iFirstValue, iLastValue) {
var iChoices = iLastValue - iFirstValue + 1;
return Math.floor(Math.random() * iChoices + iFirstValue);
}

//select from between 2 and 10
var iNum = selectFrom(2, 10);

var aColors = ["red", "green", "blue", "yellow", "black", "purple", "brown"];
var sColor = aColors[selectFrom(0, aColors.length-1)];

alert(sColor);
</script>

</body>
</html>
19 changes: 19 additions & 0 deletions edition1/ch03/StackExample.htm
@@ -0,0 +1,19 @@
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
var stack = new Array;
stack.push("red");
stack.push("green");
stack.push("yellow");
alert(stack.toString()); //outputs "red,green,yellow"
var vItem = stack.pop();
alert(vItem); //outputs "yellow"
alert(stack.toString()); //outputs "red,green"

</script>

</body>
</html>
21 changes: 21 additions & 0 deletions edition1/ch03/StaticExample.htm
@@ -0,0 +1,21 @@
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
function sayHi() {
alert("hi");
}

sayHi.alternate = function() {
alert("hola");
};

sayHi(); //outputs "hi"
sayHi.alternate(); //outputs "hola"

</script>

</body>
</html>
42 changes: 42 additions & 0 deletions edition1/ch03/StringBufferExample.htm
@@ -0,0 +1,42 @@
<html>
<head>
<title>Example</title>
</head>
<body>
<p><strong>Note:</strong> The latest versions of Firefox seem to have fixed the string concatenation problem. If you are using Firefox 1.0 or later, the string buffer may actually take longer than normal string concatenation.</p>
<script type="text/javascript">
function StringBuffer() {
this.__strings__ = new Array;
}

StringBuffer.prototype.append = function (str) {
this.__strings__.push(str);
};

StringBuffer.prototype.toString = function () {
return this.__strings__.join("");
};

var d1 = new Date();
var str = "";
for (var i=0; i < 10000; i++) {
str += "text";
}
var d2 = new Date();

document.write("Concatenation with plus: " + (d2.getTime() - d1.getTime()) + " milliseconds");

var buffer = new StringBuffer();
d1 = new Date();
for (var i=0; i < 10000; i++) {
buffer.append("text");
}
var result = buffer.toString();
d2 = new Date();

document.write("<br />Concatenation with StringBuffer: " + (d2.getTime() - d1.getTime()) + " milliseconds");

</script>

</body>
</html>
70 changes: 70 additions & 0 deletions edition1/ch04/DynamicPrototypePolygonExample.htm
@@ -0,0 +1,70 @@
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">

function Polygon(iSides) {
this.sides = iSides;

if (typeof Polygon._initialized == "undefined") {

Polygon.prototype.getArea = function () {
return 0;
};

Polygon._initialized = true;
}
}



function Triangle(iBase, iHeight) {
Polygon.call(this, 3);
this.base = iBase;
this.height = iHeight;

if (typeof Triangle._initialized == "undefined") {

Triangle.prototype.getArea = function () {
return 0.5 * this.base * this.height;
};

Triangle._initialized = true;
}
}

Triangle.prototype = new Polygon();

function Rectangle(iLength, iWidth) {
Polygon.call(this, 4);
this.length = iLength;
this.width = iWidth;

if (typeof Rectangle._initialized == "undefined") {

Rectangle.prototype.getArea = function () {
return this.length * this.width;
};

Rectangle._initialized = true;
}
}

Rectangle.prototype = new Polygon();


var triangle = new Triangle(12, 4);
var rectangle = new Rectangle(22, 10);

alert(triangle.sides);
alert(triangle.getArea());

alert(rectangle.sides);
alert(rectangle.getArea());

</script>

</body>
</html>
36 changes: 36 additions & 0 deletions edition1/ch04/HybridExample.htm
@@ -0,0 +1,36 @@
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
function ClassA(sColor) {
this.color = sColor;
}

ClassA.prototype.sayColor = function () {
alert(this.color);
};

function ClassB(sColor, sName) {
ClassA.call(this, sColor);
this.name = sName;
}

ClassB.prototype = new ClassA();

ClassB.prototype.sayName = function () {
alert(this.name);
};


var objA = new ClassA("red");
var objB = new ClassB("blue", "Nicholas");
objA.sayColor();
objB.sayColor();
objB.sayName();

</script>

</body>
</html>

0 comments on commit c66a65a

Please sign in to comment.