Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nicholas C. Zakas
committed
Dec 7, 2009
1 parent
91f15cf
commit c66a65a
Showing
315 changed files
with
12,443 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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". |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
Oops, something went wrong.