This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.js
93 lines (79 loc) · 1.84 KB
/
func.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
/*jshint node:true, newcap:false */
/**
* @fileOverview
* Func Declaration File
*
* @author Shannon Moeller
* @version 0.1.0
*/
'use strict';
/**
* Modules.
*/
var copier = require('copier');
/**
* No-op generic constructor.
*
* @constructor
*/
var Func = function() {};
/**
* Creates a child of the current function.
*
* @param {Object} prot A prototype object.
* @param {Object} stat A static object.
* @this {Func} The current function.
* @return {Function} Child function.
*/
Func.extend = function extend(prot, stat) {
// Whether to execute `this.init()`.
var init = false;
// Since `extend` is a static property of a function,
// the value of `this` here will be that function.
var parent = new this();
// A function which always returns an instance.
var Child = function F() {
var self = this;
// Call with `new` if no one else has.
if (!(self instanceof F)) {
// Don't execute the init logic.
init = false;
// Just extend the prototype chain.
self = new F();
}
if (init) {
// Execute `this.init()`.
self.init.apply(self, arguments);
} else {
// Init next time around.
init = true;
}
// Return instance.
return self;
};
// Instance members.
Child.prototype = copier(parent, prot, {
// Ensure constructor value.
constructor: Child
});
// Static members.
copier(Child, stat, {
// Bless child with superpowers.
extend: extend
});
// Our shiney new child function.
return Child;
};
/**
* Expose a useful Func.
*/
module.exports = Func.extend({
/**
* No-op default initializer.
*
* @return {Object}
*/
init: function() {
return this;
}
});