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
david
committed
Feb 2, 2012
1 parent
b108639
commit aa6fa80
Showing
7 changed files
with
146 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,3 @@ | ||
source 'http://rubygems.org' | ||
|
||
gem 'jstdutil' |
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,14 @@ | ||
GEM | ||
remote: http://rubygems.org/ | ||
specs: | ||
jstdutil (0.3.12) | ||
rake | ||
watchr | ||
rake (0.9.2.2) | ||
watchr (0.7) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
jstdutil |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
server: http://localhost:4224 | ||
|
||
load: | ||
- src/inherit.js | ||
- src/*.js | ||
|
||
test: | ||
|
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 @@ | ||
function Circle(radius) { | ||
// Uncomment to solve missing new keyword | ||
//if(!(this instanceof Circle)) { | ||
// return new Circle(radius); | ||
//} | ||
|
||
this.radius = radius; | ||
} | ||
|
||
(function (p) { | ||
p.diameter = function() { | ||
return this.radius * 2; | ||
}; | ||
|
||
p.circumference = function() { | ||
return this.diameter() * Math.PI; | ||
}; | ||
|
||
p.area = function() { | ||
return this.radius * this.radius * Math.PI; | ||
}; | ||
}(Circle.prototype)); | ||
|
||
function Sphere(radius) { | ||
Circle.call(this, radius); | ||
} | ||
|
||
Sphere.inherit(Circle); | ||
|
||
Sphere.prototype.area = function() { | ||
return 4 * this._super.area.call(this); | ||
}; | ||
|
||
//Sphere.prototype = (function () { | ||
// function F() {}; | ||
// F.prototype = Circle.prototype; | ||
// | ||
// return new F(); | ||
//}()); | ||
// | ||
//// If we forget this, it will resolve as Circle through prototype chain | ||
//Sphere.prototype.constructor = Sphere; |
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,12 @@ | ||
if(!Function.prototype.inherit) { | ||
(function() { | ||
function F() {} | ||
|
||
Function.prototype.inherit = function (superFn) { | ||
F.prototype = superFn.prototype; | ||
this.prototype = new F(); | ||
this.prototype.constructor = this; | ||
this.prototype._super = superFn.prototype; | ||
}; | ||
}()); | ||
} |
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,57 @@ | ||
TestCase("CircleTest", { | ||
"test inspect objects": function() { | ||
var circ = new Circle(6); | ||
var circ2 = { radius: 6 }; | ||
|
||
assertTrue(circ instanceof Object); | ||
assertTrue(circ instanceof Circle); | ||
assertTrue(circ2 instanceof Object); | ||
|
||
assertEquals(Circle, circ.constructor); | ||
assertEquals(Object, circ2.constructor); | ||
}, | ||
|
||
"test should create another object of same kind": function() { | ||
var circle = new Circle(6); | ||
var circle2 = new circle.constructor(9); | ||
|
||
assertEquals(circle.constructor, circle2.constructor); | ||
assertTrue(circle2 instanceof Circle); | ||
}, | ||
|
||
"test should inherit properties from Circle.prototype": function() { | ||
var circle = new Circle(6); | ||
|
||
assertEquals(12, circle.diameter()); | ||
}, | ||
|
||
"test constructor is Object when prototype is overriden": function() { | ||
function Circle() {} | ||
Circle.prototype = {}; | ||
|
||
assertEquals(Object, new Circle().constructor); | ||
}, | ||
|
||
"test calling prototype withouth 'new' returns undefined": function() { | ||
var circle = Circle(6); | ||
|
||
assertEquals("undefined", typeof circle); | ||
assertEquals(6, radius); // defined property on global scope | ||
}, | ||
|
||
"test spheres are circles in 3D": function() { | ||
var radius = 6; | ||
var sphere = new Sphere(radius); | ||
|
||
assertTrue(sphere instanceof Sphere); | ||
assertTrue(sphere instanceof Circle); | ||
assertTrue(sphere instanceof Object); | ||
assertEquals(12, sphere.diameter()); | ||
}, | ||
|
||
"test should calculate sphere area": function() { | ||
var sphere = new Sphere(3); | ||
|
||
assertEquals(113, Math.round(sphere.area())); | ||
} | ||
}); |
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,17 @@ | ||
TestCase("FunctionInheritTest", { | ||
"test should link prototypes": function() { | ||
var SubFn = function() {}; | ||
var SuperFn = function() {}; | ||
SubFn.inherit(SuperFn); | ||
|
||
assertTrue(new SubFn() instanceof SuperFn); | ||
}, | ||
|
||
"test should set up link to super": function() { | ||
var SubFn = function() {}; | ||
var SuperFn = function() {}; | ||
SubFn.inherit(SuperFn); | ||
|
||
assertEquals(SuperFn.prototype, SubFn.prototype._super); | ||
} | ||
}); |