Skip to content

Commit b3999a2

Browse files
committed
Initial commit
0 parents  commit b3999a2

11 files changed

+1045
-0
lines changed

README.md

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# my.common.js
2+
3+
Probably the fastest JS class system out there. 100% no wrappers, same perfs as hand-written pure JS classes.
4+
5+
See a little [demo] (http://myjs.fr/my-class/example/example.html).
6+
7+
My.js class system is not only a class implementation, it's also a JS class design.
8+
9+
See [how My.js classes achieve better perfs] (http://myjs.fr/my-class/).
10+
11+
12+
## Create a class
13+
14+
Assume that classes are created in the namespace `myLib`.
15+
16+
(function() {
17+
18+
var Person = my.Class({
19+
20+
STATIC: {
21+
AGE_OF_MAJORITY: 18
22+
},
23+
24+
constructor: function(name, age) {
25+
this.name = name;
26+
this.age = age;
27+
},
28+
29+
sayHello: function() {
30+
console.log('Hello from ' + this.name + '!');
31+
},
32+
33+
drinkAlcohol: function() {
34+
this.age < Person.AGE_OF_MAJORITY ?
35+
console.log('Too young! Drink milk instead!') :
36+
console.log('Whiskey or beer?');
37+
}
38+
39+
});
40+
41+
myLib.Person = Person;
42+
43+
})();
44+
45+
var john = new myLib.Person('John', 16);
46+
john.sayHello(); //log "Hello from John!"
47+
john.drinkAlcohol(); //log "Too young! Drink milk instead!"
48+
49+
50+
## Extend a class
51+
52+
(function() {
53+
54+
//Dreamer extends Person
55+
var Dreamer = my.Class(Person, {
56+
57+
constructor: function(name, age, dream) {
58+
Dreamer.Super.call(this, name, age);
59+
this.dream = dream;
60+
},
61+
62+
sayHello: function() {
63+
superSayHello.call(this);
64+
console.log('I dream of ' + this.dream + '!');
65+
},
66+
67+
wakeUp: function() {
68+
console.log('Wake up! We have to change the world today!');
69+
}
70+
71+
});
72+
73+
var superSayHello = Dreamer.Super.prototype.sayHello;
74+
75+
myLib.Dreamer = Dreamer;
76+
77+
})();
78+
79+
var sylvester = new myLib.Dreamer('Sylvester', 30, 'eating Tweety');
80+
sylvester.sayHello(); //log "Hello from Sylvester!"
81+
sylvester.dream(); //log "I dream of eating Tweety!"
82+
83+
84+
## Private methods
85+
86+
See the section "Private fields and methods" of [my article] (http://myjs.fr/my-class/).
87+
88+
89+
## Add methods to a class
90+
91+
my.extendClass(myLib.Dreamer, {
92+
93+
touchTheSky: function() {
94+
console.log('Touching the sky');
95+
},
96+
97+
reachTheStars: function() {
98+
console.log('She is so pretty!');
99+
}
100+
101+
});
102+
103+
## Implement classes
104+
105+
myLib.ImaginaryTraveler = my.Class({
106+
107+
travel: function() {
108+
console.log('Traveling on a carpet!');
109+
},
110+
111+
crossOceans: function() {
112+
console.log('Saying hi to Moby Dick!');
113+
}
114+
115+
});
116+
117+
(function() {
118+
119+
//Dreamer extends Person implements ImaginaryTraveler
120+
var Dreamer = my.Class(Person, ImaginaryTraveler, {
121+
122+
constructor: function(name, age, dream) {
123+
Dreamer.Super.call(this, name, age);
124+
this.dream = dream;
125+
},
126+
127+
...
128+
129+
});
130+
131+
myLib.Dreamer = Dreamer;
132+
133+
})();
134+
135+
var aladdin = new Dreamer('Aladdin');
136+
aladdin instanceof Person; //true
137+
aladdin instanceof ImaginaryTraveler; //false
138+
aladdin.travel();
139+
aladdin.wakeUp();
140+
aladdin.sayHello();
141+
142+
## Afraid to forget the `new` operator
143+
144+
var Person = my.Class({
145+
146+
//you can now call the constructor with or without new
147+
constructor: function(name, city) {
148+
if (!(this instanceof Person))
149+
return new Person(name, city);
150+
this.name = name;
151+
this.city = citye;
152+
}
153+
154+
});

example/EnderClass.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Klass.js - copyright @dedfat
3+
* version 1.0
4+
* https://github.com/ded/klass
5+
* Follow our software http://twitter.com/dedfat :)
6+
* MIT License
7+
*/
8+
!function (context, f) {
9+
var fnTest = /xyz/.test(function () {
10+
xyz;
11+
}) ? /\bsupr\b/ : /.*/,
12+
noop = function (){},
13+
proto = 'prototype',
14+
isFn = function (o) {
15+
return typeof o === f;
16+
};
17+
18+
function klass(o) {
19+
return extend.call(typeof o == f ? o : noop, o, 1);
20+
}
21+
22+
function wrap(k, fn, supr) {
23+
return function () {
24+
var tmp = this.supr;
25+
this.supr = supr[proto][k];
26+
var ret = fn.apply(this, arguments);
27+
this.supr = tmp;
28+
return ret;
29+
};
30+
}
31+
32+
function process(what, o, supr) {
33+
for (var k in o) {
34+
if (o.hasOwnProperty(k)) {
35+
what[k] = typeof o[k] == f
36+
&& typeof supr[proto][k] == f
37+
&& fnTest.test(o[k])
38+
? wrap(k, o[k], supr) : o[k];
39+
}
40+
}
41+
}
42+
43+
function extend(o, fromSub) {
44+
noop[proto] = this[proto];
45+
var supr = this,
46+
prototype = new noop(),
47+
isFunction = typeof o == f,
48+
_constructor = isFunction ? o : this,
49+
_methods = isFunction ? {} : o,
50+
fn = function () {
51+
if (this.initialize) {
52+
this.initialize.apply(this, arguments);
53+
} else {
54+
fromSub || isFn(o) && supr.apply(this, arguments);
55+
_constructor.apply(this, arguments);
56+
}
57+
};
58+
59+
fn.methods = function (o) {
60+
process(prototype, o, supr);
61+
fn[proto] = prototype;
62+
return this;
63+
};
64+
65+
fn.methods.call(fn, _methods).prototype.constructor = fn;
66+
67+
fn.extend = arguments.callee;
68+
fn[proto].implement = fn.statics = function (o, optFn) {
69+
o = typeof o == 'string' ? (function () {
70+
var obj = {};
71+
obj[o] = optFn;
72+
return obj;
73+
}()) : o;
74+
process(this, o, supr);
75+
return this;
76+
};
77+
78+
return fn;
79+
}
80+
81+
if (typeof module !== 'undefined' && module.exports) {
82+
module.exports = klass;
83+
} else {
84+
var old = context.klass;
85+
klass.noConflict = function () {
86+
context.klass = old;
87+
return this;
88+
};
89+
context.klass = klass;
90+
}
91+
92+
}(this, 'function');

example/EnderTest.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
EnderPerson = klass(function(name) {
2+
this.name = name;
3+
})
4+
.methods({
5+
setAddress: function(country, city, street) {
6+
this.country = country;
7+
this.city = city;
8+
this.street = street;
9+
},
10+
sayHello: function() {
11+
console.log('I am ' + this.name + '. My address is ' +
12+
this.country + ', ' + this.city + ', ' + this.street + '.');
13+
}
14+
});
15+
16+
EnderFrenchGuy = EnderPerson.extend(function(name) {
17+
})
18+
.methods({
19+
setAddress: function(city, street) {
20+
this.supr('France', city, street);
21+
}
22+
});
23+
24+
25+
EnderParisLover = EnderFrenchGuy.extend(function(name) {
26+
})
27+
.methods({
28+
setAddress: function(street) {
29+
this.supr('Paris', street);
30+
}
31+
});

example/JRClass.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
(function(){
2+
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
3+
// The base Class implementation (does nothing)
4+
this.JRClass = function(){};
5+
6+
// Create a new Class that inherits from this class
7+
JRClass.extend = function(prop) {
8+
var _super = this.prototype;
9+
10+
// Instantiate a base class (but only create the instance,
11+
// don't run the init constructor)
12+
initializing = true;
13+
var prototype = new this();
14+
initializing = false;
15+
16+
// Copy the properties over onto the new prototype
17+
for (var name in prop) {
18+
// Check if we're overwriting an existing function
19+
prototype[name] = typeof prop[name] == "function" &&
20+
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
21+
(function(name, fn){
22+
return function() {
23+
var tmp = this._super;
24+
25+
// Add a new ._super() method that is the same method
26+
// but on the super-class
27+
this._super = _super[name];
28+
29+
// The method only need to be bound temporarily, so we
30+
// remove it when we're done executing
31+
var ret = fn.apply(this, arguments);
32+
this._super = tmp;
33+
34+
return ret;
35+
};
36+
})(name, prop[name]) :
37+
prop[name];
38+
}
39+
40+
// The dummy class constructor
41+
function JRClass() {
42+
// All construction is actually done in the init method
43+
if ( !initializing && this.init )
44+
this.init.apply(this, arguments);
45+
}
46+
47+
// Populate our constructed prototype object
48+
JRClass.prototype = prototype;
49+
50+
// Enforce the constructor to be what we expect
51+
JRClass.constructor = JRClass;
52+
53+
// And make this class extendable
54+
JRClass.extend = arguments.callee;
55+
56+
return JRClass;
57+
};
58+
})();

example/JRTest.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
window.JRPerson = JRClass.extend({
2+
init: function(name){
3+
this.name = name;
4+
},
5+
setAddress: function(country, city, street) {
6+
this.country = country;
7+
this.city = city;
8+
this.street = street;
9+
},
10+
sayHello: function() {
11+
console.log('I am ' + this.name + '. My address is ' +
12+
this.country + ', ' + this.city + ', ' + this.street + '.');
13+
}
14+
});
15+
16+
window.JRFrenchGuy = JRPerson.extend({
17+
init: function(name) {
18+
this._super(name);
19+
},
20+
setAddress: function(city, street) {
21+
this._super('France', city, street);
22+
}
23+
});
24+
25+
window.JRParisLover = JRFrenchGuy.extend({
26+
init: function(name) {
27+
this._super(name);
28+
},
29+
setAddress: function(street) {
30+
this._super('Paris', street);
31+
}
32+
});
33+

0 commit comments

Comments
 (0)