-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.js
executable file
·99 lines (96 loc) · 3.63 KB
/
class.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
(function(global) {
/**
* This function checks passed value is function type is or not
* @param {any} obj which you want to check
* @returns true if passed object is function type otherwise false
*/
var isFunction = function(obj) {
return obj && typeof obj === "function";
};
/**
* Used to seperate class variables and methods
*
* @param {Object} obj
* @returns {Object} Seperate object
*/
var seperateProperties = function (obj) {
if (typeof obj === 'object') {
var seperatedPprops = {
methods: {},
properties: {},
};
var paramKeys = Object.keys(obj);
for (var index = 0; index < paramKeys.length; index++) {
if (isFunction(obj[paramKeys[index]])) {
seperatedPprops.methods[paramKeys[index]] = obj[paramKeys[index]];
} else {
seperatedPprops.properties[paramKeys[index]] = obj[paramKeys[index]];
}
}
return seperatedPprops;
} else {
console.error('Class parameter should be Object');
}
return;
};
/**
* Main function classRefrence called when user will decelare class using new keyword
*
* Called init function which decelares all variables
*/
function classRefrence() {
setClassVariables.call(this, this.classProperties);
init.apply(this, arguments);
}
/**
* Custom init which defines all function properties
*
* @param {Object} obj
*/
var init = function () {
var objectArguments = arguments[0];
if (objectArguments && (typeof objectArguments === 'object')) {
var propertiesNames = Object.keys(objectArguments);
for (var index = 0; index < propertiesNames.length; index++) {
if (this.hasOwnProperty(propertiesNames[index])) {
this[propertiesNames[index]] = objectArguments[propertiesNames[index]];
}
}
} else {
console.error('Class parameter should be Object');
}
};
var setClassVariables = function () {
var classVariables = arguments[0];
var variableNames = Object.keys(classVariables);
if (classVariables && variableNames.length) {
for (var index = 0; index < variableNames.length; index++) {
this[variableNames[index]] = classVariables[variableNames[index]];
}
}
};
function Class() {
var seperatedClassPprops = seperateProperties(arguments.length && arguments[0]);
if (seperatedClassPprops) {
seperatedClassPprops.methods.init = seperatedClassPprops.methods.init || init.bind(this, seperatedClassPprops.properties);
// setClassVariables.call(classRefrence, seperatedClassPprops.properties);
classRefrence.prototype = new Properties(seperatedClassPprops);
} else {
console.error('Wrong class arguments.');
}
return classRefrence;
}
function Properties() {
var objMethods = arguments[0]['methods'];
// Object.prototype.toString
var all_keys = Object.keys(objMethods);
for (var index = 0; index < all_keys.length; index++) {
if (objMethods && isFunction(objMethods[all_keys[index]])) {
this[all_keys[index]] = objMethods[all_keys[index]];
}
}
this['classProperties'] = arguments[0]['properties'];
}
Properties.prototype.extend = function() {};
global.Class = Class;
})(window);