SoFunction
Updated on 2025-04-10

[Transfer] Prototype Source Code Interpretation Super Recommendation Page 1/3


/**   
2   
3  * Define a global object, the property Version will be replaced with the current version number when it is released
4   
5  */   
6    
7 var Prototype = {    
8    
9   Version: '@@VERSION@@'   
10    
11 }    
12    
13    
14 /**   
15   
16  * Create a type, note that its attributes create is a method that returns a constructor.
17   
18  * Generally used as follows
19   
20  *     var X = ();  Returns a type, similar to one of java
21   
22 * Class instance.
23   
24  * To use the X  type, you need to continue to use new X() to get an instance, just like java
25   
26  * () method.
27   
28  *   
29   
30  * The returned constructor will execute a method called initialize, initialize is
31   
32 * Ruby object's constructor method name.
33   
34  * At this time, the initialize method has not been defined yet, and a new type will be created in the subsequent code.
35   
36  * The corresponding method of the same name.
37   
38  *   
39   
40  * If you have to understand it from Java. You can understand it as creating a using ()
41   
42  * Inherited class.
43   
44  * Of course java does not allow this, because the Class class is final
45   
46  *   
47   
48  */   
49    
50 var Class = {    
51    
52   create: function() {    
53    
54     return function() {    
55    
56       (this, arguments);    
57    
58     }    
59    
60   }    
61    
62 }    
63    
64    
65 /**   
66   
67  * Create an object, think from the variable name, the original intention may be to define an abstract class, and create it later
68   
69  * New object all extend it.
70   
71  * But from the application of subsequent codes, Abstract is more about keeping the namespace clear.
72   
73  * In other words, we can add a new object definition to the Abstract object instance.
74   
75  *   
76   
77  * Understanding from Java, it is to dynamically create an internal class for an object.
78   
79  */   
80    
81 var Abstract = new Object();    
82    
83    
84 /**   
85   
86  * Getting all properties and methods of parameter objects is a bit like multiple inheritance. But this inheritance is obtained dynamically.
87   
88  * For example:
89   
90  *     var a = new ObjectA(), b = new ObjectB();   
91   
92  *     var c = (b);   
93   
94  * At this time, object c has both properties and methods of objects a  and b. But unlike multiple inheritance,
95   
96  * c instanceof ObjectB  will return false.
97   
98  */   
99    
100  = function(object) {    
101    
102   for (property in object) {    
103    
104     this[property] = object[property];    
105    
106   }    
107    
108   return this;    
109    
110 }    
111    
112    
113 /**   
114   
115  * This method is very interesting. It encapsulates a javascript function object, returns a new function object, a new function
116   
117  * The main body of the number object is the same as the original object, but the bind() method parameter will be used as the object of the current object.
118   
119  * In other words, this reference in the new function is changed to an object provided by the parameter.
120   
121  * For example:
122   
123  * <input type="text"  value="aaa">   
124   
125  * <input type="text"  value="bbb">   
126   
127  * .................   
128   
129  * <script>   
130   
131  *     var aaa = ("aaa");   
132   
133  *     var bbb = ("bbb");   
134   
135  *      = function() {alert();}   
136   
137  *     aaa.showValue2 = (bbb);   
138   
139  * </script>   
140   
141  *  Then, the call will return "aaa",
142   
143  *  But calling aaa.showValue2 will return "bbb".
144   
145  *   
146   
147  * apply is a new method that only appeared after ie5.5 (Netscape seems to have supported it very early).
148   
149  * For more information on this method, please refer to MSDN
150   
151  * /library/en-us/script56/html/   
152   
153  * There is also a call method, which is similar to apply. You can study it together.
154   
155  */   
156    
157  = function(object) {    
158    
159   var method = this;    
160    
161   return function() {    
162    
163     (object, arguments);    
164    
165   }    
166    
167 }    
168    
169    
170 /**   
171   
172  * Same as bind, but this method is generally used as event processing for html control objects. So you have to pass the event object
173   
174  * Note that at this time, I used it. Its difference seems to be just a comparison
175   
176  * Definition of number form. Just like java two overload methods.
177   
178  */   
179    
180  = function(object) {    
181    
182   var method = this;    
183    
184   return function(event) {    
185    
186     (object, event || );    
187    
188   }    
189    
190 }    
191    
192    
193 /**   
194   
195  * Convert the integer form RGB color value to HEX form
196   
197  */   
198    
199  = function() {    
200    
201   var digits = (16);    
202    
203   if (this < 16) return '0' + digits;    
204    
205   return digits;    
206    
207 }    
208    
209    
210 /**   
211   
212  * Typical Ruby-style function calls methods in parameters one by one, returning the return value of the first successfully executed method
213   
214  */   
215    
216 var Try = {    
217    
218   these: function() {    
219    
220     var returnValue;    
221    
222    
223     for (var i = 0; i < ; i++) {    
224    
225       var lambda = arguments[i];    
226    
227       try {    
228    
229         returnValue = lambda();    
230    
231         break;    
232    
233       } catch (e) {}    
234    
235     }    
236    
237    
238     return returnValue;    
239    
240   }    
241    
242 }    
243    
244    
245 /*--------------------------------------------------------------------------*/   
246    
247    
248 /**   
249   
250  * A well-designed timing actuator
251   
252  * First create a PeriodicalExecuter type from (),
253   
254  * Then set the prototype using the syntax form of the object direct quantity.
255   
256  *   
257   
258  * The one that needs to be specifically noted is the rgisterCallback method, which calls the function prototype method bind defined above,
259   
260  * and pass yourself as parameters.
261   
262 * The reason for this is that setTimeout by default, always takes the window object as the current object, that is,
263   
264  * If the registerCallback method is defined as follows:
265   
266  *     registerCallback: function() {   
267   
268  *         setTimeout(,  * 1000);   
269   
270  *     }   
271   
272 * Then, the method execution fails because it cannot
273   
274  * Access   Properties.
275   
276  *
277   
278  *  That is the current instance of PeriodicalExecuter.
279   
280  */   
281    
282 var PeriodicalExecuter = ();    
283    
284  = {    
285    
286   initialize: function(callback, frequency) {    
287    
288      = callback;    
289    
290      = frequency;    
291    
292      = false;    
293    
294    
295     ();    
296    
297   },    
298    
299    
300   registerCallback: function() {    
301    
302     setTimeout((this),  * 1000);    
303    
304   },    
305    
306    
307   onTimerEvent: function() {    
308    
309     if (!) {    
310    
311       try {    
312    
313          = true;    
314    
315         ();    
316    
317       } finally {    
318    
319          = false;    
320    
321       }    
322    
323     }    
324    
325    
326     ();    
327    
328   }    
329    
330 }    
331    
332    
333 /*--------------------------------------------------------------------------*/   
334    
335    
336 /**   
337   
338  * This function is Ruby. I think it has two main functions
339   
340  * 1.  It is probably the simplified call of (id).
341   
342 * For example: $("aaa") will return the aaa object
343   
344 * 2. Get the object array
345   
346  * For example: $("aaa","bbb") Returns a id including id
347   
348  * Array of two input control objects "aaa" and "bbb".
349   
350  */   
351    
352 function $() {    
353    
354   var elements = new Array();    
355    
356    
357   for (var i = 0; i < ; i++) {    
358    
359     var element = arguments[i];    
360    
361     if (typeof element == 'string')    
362    
363       element = (element);    
364    
365    
366     if ( == 1)    
367    
368       return element;    
369    
370    
371     (element);    
372    
373   }    
374    
375    
376   return elements;    
377    
378 }