Skip to content

Commit

Permalink
Implementing _super function
Browse files Browse the repository at this point in the history
  • Loading branch information
david committed Feb 2, 2012
1 parent b108639 commit aa6fa80
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source 'http://rubygems.org'

gem 'jstdutil'
14 changes: 14 additions & 0 deletions Gemfile.lock
@@ -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
1 change: 1 addition & 0 deletions jsTestDriver.conf
@@ -1,6 +1,7 @@
server: http://localhost:4224

load:
- src/inherit.js
- src/*.js

test:
Expand Down
42 changes: 42 additions & 0 deletions src/circle.js
@@ -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;
12 changes: 12 additions & 0 deletions src/inherit.js
@@ -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;
};
}());
}
57 changes: 57 additions & 0 deletions test/circle_test.js
@@ -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()));
}
});
17 changes: 17 additions & 0 deletions test/function_inherit_test.js
@@ -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);
}
});

0 comments on commit aa6fa80

Please sign in to comment.