class.extend
Advanced tools
Comparing version 0.9.1 to 0.9.2
(function(){ | ||
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/; | ||
// The base Class implementation (does nothing) | ||
this.Class = function(){}; | ||
// Create a new Class that inherits from this class | ||
Class.extend = function(prop) { | ||
Class.extend = function(className, prop) { | ||
if(prop == undefined) { | ||
prop = className; | ||
className = "Class"; | ||
} | ||
var _super = this.prototype; | ||
// Instantiate a base class (but only create the instance, | ||
@@ -16,3 +21,3 @@ // don't run the init constructor) | ||
initializing = false; | ||
// Copy the properties over onto the new prototype | ||
@@ -26,12 +31,12 @@ for (var name in prop) { | ||
var tmp = this._super; | ||
// Add a new ._super() method that is the same method | ||
// but on the super-class | ||
this._super = _super[name]; | ||
// The method only need to be bound temporarily, so we | ||
// remove it when we're done executing | ||
var ret = fn.apply(this, arguments); | ||
var ret = fn.apply(this, arguments); | ||
this._super = tmp; | ||
return ret; | ||
@@ -42,3 +47,3 @@ }; | ||
} | ||
// The dummy class constructor | ||
@@ -50,12 +55,15 @@ function Class() { | ||
} | ||
// Populate our constructed prototype object | ||
Class.prototype = prototype; | ||
// Enforce the constructor to be what we expect | ||
Class.prototype.constructor = Class; | ||
var func = new Function( | ||
"return function " + className + "(){ }" | ||
)(); | ||
Class.prototype.constructor = func; | ||
// And make this class extendable | ||
Class.extend = arguments.callee; | ||
return Class; | ||
@@ -62,0 +70,0 @@ }; |
{ | ||
"name": "class.extend", | ||
"description": "copy/paste implementation of John Resig's Simple JavaScript inheritance.", | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"repository": { | ||
@@ -6,0 +6,0 @@ "type": "git", |
@@ -13,3 +13,3 @@ class.extend | ||
var Person = Class.extend({ | ||
var Person = Class.extend('Person', { | ||
init: function(isDancing){ | ||
@@ -23,3 +23,3 @@ this.dancing = isDancing; | ||
var Ninja = Person.extend({ | ||
var Ninja = Person.extend('Ninja', { | ||
init: function(){ | ||
@@ -26,0 +26,0 @@ this._super( false ); |
@@ -5,3 +5,3 @@ require("should"); | ||
describe('Mode Parser', function() { | ||
it('should parse 0777',function() { | ||
it('should have correct instanceof result',function() { | ||
var Person = Class.extend({ | ||
@@ -43,2 +43,43 @@ init: function(isDancing){ | ||
}); | ||
it('should have correct contructor name',function() { | ||
var Person = Class.extend('Person', { | ||
init: function(isDancing){ | ||
this.dancing = isDancing; | ||
}, | ||
dance: function(){ | ||
return this.dancing; | ||
} | ||
}); | ||
var Ninja = Person.extend('Ninja', { | ||
init: function(){ | ||
this._super( false ); | ||
}, | ||
dance: function(){ | ||
// Call the inherited version of dance() | ||
return this._super(); | ||
}, | ||
swingSword: function(){ | ||
return true; | ||
} | ||
}); | ||
var p = new Person(true); | ||
p.dance().should.be.true; | ||
var n = new Ninja(); | ||
n.dance().should.be.false; | ||
n.swingSword().should.be.true; | ||
// Should all be true | ||
(p instanceof Person).should.be.true; | ||
(p instanceof Class).should.be.true; | ||
(n instanceof Ninja).should.be.true; | ||
(n instanceof Person).should.be.true; | ||
(n instanceof Class).should.be.true; | ||
p.constructor.name.should.be.equal('Person'); | ||
n.constructor.name.should.be.equal('Ninja'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5615
128