Permalink
Browse files

Made class variables able to inherit. And added a class variable inhe…

…ritance test.
  • Loading branch information...
1 parent 7c2b4b1 commit 2fc196d2d21d0d369bd4a911bc06d22947d77171 Heungsub Lee committed with Jun 25, 2010
Showing with 40 additions and 8 deletions.
  1. +15 −8 classy.js
  2. +25 −0 tests/core.js
View
23 classy.js
@@ -1,7 +1,7 @@
/**
* Classy - classy classes for JavaScript
*
- * :copyright: (c) 2010 by Armin Ronacher.
+ * :copyright: (c) 2010 by Armin Ronacher.
* :license: BSD.
*/
@@ -83,12 +83,20 @@
prototype[name] = mixin[name];
}
}
+
+ /* copy class vars from the superclass */
+ properties.__classvars__ = properties.__classvars__ || {};
+ if (prototype.__classvars__)
+ for (var key in prototype.__classvars__)
+ if (!properties.__classvars__[key]) {
+ var value = getOwnProperty(prototype.__classvars__, key);
+ properties.__classvars__[key] = value;
+ }
/* copy all properties over to the new prototype */
for (var name in properties) {
var value = getOwnProperty(properties, name);
if (name === '__include__' ||
- name === '__classvars__' ||
value === undefined)
continue;
@@ -119,12 +127,11 @@
}
/* copy all class vars over of any */
- if (properties.__classvars__)
- for (var key in properties.__classvars__) {
- var value = getOwnProperty(properties.__classvars__, key);
- if (value !== undefined)
- rv[key] = value;
- }
+ for (var key in properties.__classvars__) {
+ var value = getOwnProperty(properties.__classvars__, key);
+ if (value !== undefined)
+ rv[key] = value;
+ }
/* copy prototype and constructor over, reattach $extend and
return the class */
View
25 tests/core.js
@@ -195,3 +195,28 @@ test('$class gives class access', function() {
});
equal(Test().$class.classattr, 42, 'classattr is 42');
});
+
+test('class variable inheritance', function() {
+ var Test = Class.$extend({
+ __classvars__ : {
+ foo: 23,
+ bar: 'test'
+ }
+ });
+ var SubTest = Test.$extend({
+ __classvars__ : {
+ bar: 'subtest'
+ }
+ });
+ var SubSubTest = SubTest.$extend({
+ __classvars__ : {
+ foo: 999
+ }
+ });
+
+ ok(SubTest.foo, 'SubTest also has a foo');
+ equal(SubTest.foo, Test.foo, 'SubTest.foo is Test.foo');
+ equal(SubTest.bar, 'subtest', 'SubTest.bar has been overridden');
+ equal(SubSubTest.bar, SubTest.bar, 'SubSubTest.bar is Test.bar');
+ equal(SubSubTest.foo, 999, 'SubSubTest.foo has been overridden');
+});

0 comments on commit 2fc196d

Please sign in to comment.