-
Notifications
You must be signed in to change notification settings - Fork 38
/
emcascript.js
146 lines (143 loc) · 3.73 KB
/
emcascript.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
Function.prototype.mybind = function(context, ...args) {
let fun = this;
function bound(...args2) {
let self = this instanceof bound ? this : context;
return fun.apply(self, args.concat(args2));
}
bound.prototype = Object.create(fun.prototype);
return bound;
};
Function.prototype.mycall = function(context, ...args) {
context.fun = this;
return context.fun(...args);
};
Function.prototype.myapply = function(context, args) {
context.fun = this;
return context.fun(...args);
};
function isInstanceOf(child, fun) {
if (typeof fun !== "function") {
throw new TypeError("arg2 fun is not a function");
}
if (child === null) {
return false;
}
if (child.__proto__ !== fun.prototype) {
return isInstanceOf(child.__proto__, fun);
}
return true;
}
function myNew(fun, ...arg) {
if (typeof fun !== "function") {
throw new TypeError(" fun is not a function");
}
let obj = {};
Object.setPrototypeOf(obj, fun.prototype);
fun.apply(obj, arg);
return obj;
}
function myExtends(parent, child) {
function nop() {}
nop.prototype = parent.prototype;
child.prototype = new nop();
}
function fromateNum() {}
function parseValue(str) {
if (str === "undefined") {
return undefined;
}
if (str === "null") {
return null;
}
if (str === "true") {
return true;
}
if (str === "false") {
return false;
}
if (str[0] === "{") {
return JSONParse(str);
}
if (str.indexOf(".") !== -1) {
let value = parseFloat(str);
if (!isNaN(value)) {
return value;
}
}
let value = parseInt(str);
if (!isNaN(value)) {
return value;
}
return str;
}
function JSONParse(strs) {
if (strs === "" || typeof strs !== "string") {
throw new SyntaxError("JSONParse error");
}
if (strs[0] === "{") {
let obj = {};
if (strs[strs.length - 1] == "}") {
let fields = strs.substring(1, strs.length - 1).split(",");
for (let field of fields) {
let index = field.indexOf(":");
let temp = [];
if (index !== -1) {
temp[0] = field.substring(0, index);
temp[1] = field.substring(index + 1, field.length);
}
let key = temp[0].substring(1, temp[0].length - 1);
let value = parseValue(temp[1]);
//if (value !== undefined) {
obj[key] = value;
//}
}
}
console.log("prase:", obj);
return obj;
}
if (strs[0] === "[") {
if (strs[strs.length - 1] == "]") {
let result = [];
let fields = strs.substring(1, strs.length - 1).split(",");
for (let field of fields) {
result.push(parseValue(field));
}
return result;
}
}
}
function JSONStringify(obj) {
if (
obj === undefined ||
obj === null ||
typeof obj === "string" ||
typeof obj === "boolean" ||
typeof obj === "number"
) {
return obj;
}
if (typeof obj === "function") {
return "";
}
if (Array.isArray(obj)) {
let result = [];
for (let i = 0; i < obj.length; i++) {
result.push(JSONStringify(obj[i]));
}
return "[" + result.join(",") + "]";
} else {
let result = [];
for (let key in obj) {
result.push(`"${key}":${JSONStringify(obj[key])}`);
}
return "{" + result.join(",") + "}";
}
}
const emca = {
isInstanceOf,
JSONParse,
JSONStringify,
myNew,
myExtends
};
export { emca };