-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.js
183 lines (166 loc) · 6.06 KB
/
template.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*jslint browser: true, devel: true, maxlen: 82 */
/* jshint bitwise: true, curly: true, eqeqeq: true, es3: false,
forin: true, freeze: true, futurehostile: true, latedef: true,
maxcomplexity: 8, maxstatements: 35, noarg: true, nocomma: true,
noempty: true, nonew: true, singleGroups: true, undef: true, unused: true,
plusplus: true, strict: true, browser: true, devel: true
*/
// When jQuery (or some other libraries) is being used
/*global $, jQuery */
// Start things off when the DOM is ready
$(function () {
"use strict";
var myConfig = {};
// Use an inner self-running anonymous function to really kick things off
// if need to pass any configuration information on startup.
(function (config) {
return config;
}(myConfig));// ./function(config)
//////////////////////////////
// Object Decorator Pattern //
//////////////////////////////
/**
* Add functionality to an existing object
*
* Takes in an object, and one or more properties to it, and returns
* the enhanced (decorated) object.
*
* name as the category of [real world] object being created, plus an
* adjective describing the operation
*/
var noun1Like = function (obj) {// , parameterList…
obj.propertyName = {};// literal, calculation, function, object
return obj;
};
// Starting from object, or with existing properties
var myObj1a = noun1Like({});// , argumentList…
var myObj1b = noun1Like({a: 10});
//////////////////////////////
// Functional Class Pattern //
//////////////////////////////
/**
* Noun2 (Class) constructor function
*/
var Noun2 = function () {// parameterList…
var nounInstance = {prop1: 1};// properties: constants | parameters
nounInstance.method1 = function () {
// nounInstance from closure scope; new value or calculation
nounInstance.prop2 = {};
};
return nounInstance;
};
var myObj2a = Noun2();// argumentList…
///////////////////////////////
// Functional Shared Pattern //
///////////////////////////////
/**
* method to be run on any Noun3 instance
*/
var noun3Method1 = function () {// parameterList…
this.propertyName = {};// new value, maybe new property
};
/**
* Noun3 (Class) constructor function
*
* functional class pattern with shared methods
*/
var Noun3 = function () {// parameterList…
var nounInstance = {prop1: 1};// properties: constants or from parameters
nounInstance.method1 = noun3Method1;
//nounInstance.methodN = nounMethodN;
jQuery.extend(nounInstance, Noun3.methods);
return nounInstance;
};
Noun3.methods = {
method1: function () {// parameterList…
this.propertyName = {};// new value, maybe new property
}
// , methodn : function () {}
};
var myObj3a = Noun3();// argumentList…
//////////////////////////////
// Prototypal Class Pattern //
//////////////////////////////
/**
* Noun4 (Class) constructor function
*/
var Noun4 = function () {// parameterList…
var nounInstance = Object.create(Noun4.methods);
nounInstance.prop1 = {};// properties: constants or from parameters
return nounInstance;
};
Noun4.methods = {
method1: function () {// parameterList…
this.propertyName = {};// new value, maybe new property
}
// , methodn : function () {}
};
var myObj4a = Noun4();// argumentList…
/**
* Noun5 (Class) constructor function
*
* using the automatically supplied .prototype property
*/
var Noun5 = function () {// parameterList…
var nounInstance = Object.create(Noun5.prototype);
nounInstance.prop1 = {};// properties: constants or from parameters
return nounInstance;
};
Noun5.prototype.method1 = function () {// parameterList…
this.propertyName = {};// new value, maybe new property
};
var myObj5a = Noun5();// argumentList…
/////////////////////////////
// Pseudoclassical Pattern //
/////////////////////////////
/**
* Noun6 (Class constructor function)
*
* Syntactic sugar, but interpreter does optimizations
*/
var Noun6 = function () {// parameterList…
// auto provided (invisible) code from 'new'
// this = Object.create(Noun6.prototype);
this.prop1 = {};// properties: constants or from parameters
// auto provided (invisible) code from 'new'
// return this;
};
Noun6.prototype.method1 = function () {// parameterList…
this.propertyName = {};// new value, maybe new property
};
var myObj6a = new Noun6();// argumentList…
/////////////////////////////////
// Pseudoclassical Subclassing //
/////////////////////////////////
/**
* Superclass1 constructor function)
*
* Normal Pseudoclassical Pattern
*/
var Superclass1 = function () {// parameterList…
this.prop1 = {};// properties: constants or from parameters
};
Superclass1.prototype.method1 = function () {// parameterList…
this.propertyName = {};// new value, maybe new property
};
/**
* SubClass1 constructor function
*/
var Subclass1 = function () {// parameterList…
Superclass1.call(this);// , parameterList…
// These could override properties of the superclass
this.prop2 = {};// properties: constants or from parameters
};
Subclass1.prototype = Object.create(Superclass1.prototype);
Subclass1.prototype.constructor = Subclass1;
// These could override methods of the superclass
Subclass1.prototype.method2 = function () {// parameterList…
this.propertyName = {};// new value, maybe new property
};
var myObjS1a = new Subclass1();// argumentList…
//Reduce jslint complaints for the template code
return [
myObj1a, myObj1b, myObj2a, myObj3a, myObj4a, myObj5a, myObj6a,
myObjS1a
];
});// ./function ()