Skip to content

Commit 429f48f

Browse files
author
OJay Robinson
committed
Add Grant class files extend and promote to createjs/utils
1 parent a47d6b2 commit 429f48f

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

src/createjs/utils/extend.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* core
3+
* Visit http://createjs.com/ for documentation, updates and examples.
4+
*
5+
* Copyright (c) 2010 gskinner.com, inc.
6+
*
7+
* Permission is hereby granted, free of charge, to any person
8+
* obtaining a copy of this software and associated documentation
9+
* files (the "Software"), to deal in the Software without
10+
* restriction, including without limitation the rights to use,
11+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the
13+
* Software is furnished to do so, subject to the following
14+
* conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be
17+
* included in all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26+
* OTHER DEALINGS IN THE SOFTWARE.
27+
*/
28+
29+
/**
30+
* @module CreateJS
31+
*/
32+
"use strict";
33+
34+
// namespace:
35+
this.createjs = this.createjs||{};
36+
37+
/**
38+
* @class Utility Methods
39+
*/
40+
41+
/**
42+
* Sets up the prototype chain and constructor property for a new class.
43+
*
44+
* This should be called right after creating the class constructor.
45+
*
46+
* function MySubClass() {}
47+
* createjs.extend(MySubClass, MySuperClass); // returns the prototype
48+
* ClassB.prototype.doSomething = function() { }
49+
*
50+
* var foo = new MySubClass();
51+
* console.log(foo instanceof MySuperClass); // true
52+
* console.log(foo.prototype.constructor === MySubClass); // true
53+
*
54+
* @method extends
55+
* @param {Function} subclass The subclass.
56+
* @param {Function} superclass The superclass to extend.
57+
* @return {Function} Returns the subclass's new prototype.
58+
*/
59+
createjs.extend = function(subclass, superclass) {
60+
function o() { this.constructor = subclass; }
61+
o.prototype = superclass.prototype;
62+
return (subclass.prototype = new o());
63+
};

src/createjs/utils/promote.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* core
3+
* Visit http://createjs.com/ for documentation, updates and examples.
4+
*
5+
* Copyright (c) 2010 gskinner.com, inc.
6+
*
7+
* Permission is hereby granted, free of charge, to any person
8+
* obtaining a copy of this software and associated documentation
9+
* files (the "Software"), to deal in the Software without
10+
* restriction, including without limitation the rights to use,
11+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the
13+
* Software is furnished to do so, subject to the following
14+
* conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be
17+
* included in all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26+
* OTHER DEALINGS IN THE SOFTWARE.
27+
*/
28+
29+
/**
30+
* @module CreateJS
31+
*/
32+
"use strict";
33+
34+
// namespace:
35+
this.createjs = this.createjs||{};
36+
37+
/**
38+
* @class Utility Methods
39+
*/
40+
41+
/**
42+
* Promotes any methods on the super class that were overridden, by creating an alias in the format `SuperClassName_methodName`.
43+
* An alias to the super class's constructor is always added in the format `SuperClassName_constructor`.
44+
* This allows the subclass to call super class methods without using `function.call`, providing better performance.
45+
*
46+
* For example, if `MySubClass` extends `MySuperClass`, and both define a `draw` method, then calling `promote(MySubClass)`
47+
* would: add a `MySuperClass_constructor` method to MySubClass and promote the `draw` method on MySuperClass to the
48+
* prototype of MySubClass as `MySuperClass_draw`.
49+
*
50+
* This should be called after the class's prototype is fully defined.
51+
*
52+
* function ClassA(name) {
53+
* this.name = name;
54+
* }
55+
* ClassA.prototype.greet = function() {
56+
* return "Hello "+this.name;
57+
* }
58+
*
59+
* function ClassB(name, punctuation) {
60+
* this.ClassA_constructor(name);
61+
* this.punctuation = punctuation;
62+
* }
63+
* createjs.extend(ClassB, ClassA);
64+
* ClassB.prototype.greet = function() {
65+
* return this.ClassA_greet()+this.punctuation;
66+
* }
67+
* createjs.promote(ClassB);
68+
*
69+
* var foo = new ClassB("World", "!?!");
70+
* console.log(foo.greet()); // Hello World!?!
71+
*
72+
* @method extends
73+
* @param {Function} subclass The subclass to promote super class methods on.
74+
* @param {String} [superclassName] The name of the superclass. This is only necessary if the constructor is an anonymous function (`MyClass = function()` instead of `function MyClass()`).
75+
* @return {Function} Returns the subclass.
76+
*/
77+
createjs.promote = function(subclass, superclassName) {
78+
var subP = subclass.prototype, supP = (Object.getPrototypeOf&&Object.getPrototypeOf(subP))||subP.__proto__;
79+
if (supP) {
80+
superclassName = superclassName || supP.constructor.name || /^function\s+([^\s\(]+)\s*\(/.exec(String(supP.constructor))[1];
81+
82+
subP[superclassName + "_constructor"] = supP.constructor; // constructor is not always innumerable
83+
for (var n in supP) {
84+
if (subP[n] && (typeof supP[n] == "function")) { subP[superclassName + "_" + n] = supP[n]; }
85+
}
86+
}
87+
return subclass;
88+
};

0 commit comments

Comments
 (0)