-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclass.js
More file actions
94 lines (86 loc) · 3.26 KB
/
Copy pathclass.js
File metadata and controls
94 lines (86 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Make a new Class:
var Person = Sage.Class.define({
constructor: function(str) {
this.name = str;
},
iAm: function() {
return this.name;
}
});
To create a class which inherits from an already existing one
just call the already-existing class' extend() method: **
var Knight = Person.extend({
iAm: function() {
return 'Sir ' + this.base();
},
joust: function() {
return 'Yaaaaaa!';
}
});
Notice the Knight's iAm() method has access to it's 'super'
via this.base();
** differs from the Ext method of having to pass in the parent,
** ours defines the extend() method directly on every defined class
*/
/*global Sage $ alert*/
if(window.Sage) {
(function(S) {
var INITIALIZING = false,
// straight outta base2
OVERRIDE = /xyz/.test(function(){xyz;}) ? /\bbase\b/ : /.*/;
// The base Class placeholder
S.Class = function(){};
// Create a new Class that inherits from this class
S.Class.define = function define(prop) {
var base = this.prototype;
// Instantiate a base class (but only create the instance)
INITIALIZING = true;
var prototype = new this();
INITIALIZING = false;
var wrap = function(name, fn) {
return function() {
var tmp = this.base;
// Add a new .base() method that is the same method
// but on the base class
this.base = base[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this.base = tmp;
return ret;
};
};
// Copy the properties over onto the new prototype
var hidden = ['constructor'],
i = 0,
name;
for (name in prop) {
// Check if we're overwriting an existing function
prototype[name] = typeof prop[name] === "function" &&
typeof base[name] === "function" &&
OVERRIDE.test(prop[name]) ? wrap(name, prop[name]) : prop[name];
}
while (name = hidden[i++])
if (prop[name] != base[name])
prototype[name] = typeof prop[name] === "function" &&
typeof base[name] === "function" &&
OVERRIDE.test(prop[name]) ? wrap(name, prop[name]) : prop[name];
// The dummy class constructor
function Class() {
// All construction is actually done in the initialize method
if ( !INITIALIZING && this.constructor ) {
this.constructor.apply(this, arguments);
}
}
// Populate the constructed prototype object
Class.prototype = prototype;
// Enforce the constructor to be what we expect
Class.constructor = Class;
// And make this class 'define-able'
Class.define = define;
Class.extend = Class.define; // sounds better for inherited classes
return Class;
};
}(window.Sage));
}