Skip to content

Commit a3d143f

Browse files
committed
feat: babel class transform to function
1 parent 141f6b1 commit a3d143f

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

Diff for: babel/classToES5.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"use strict";
2+
3+
function _instanceof(left, right) {
4+
// ES6 原型检查
5+
if (
6+
right != null &&
7+
typeof Symbol !== "undefined" &&
8+
right[Symbol.hasInstance]
9+
) {
10+
return right[Symbol.hasInstance](left);
11+
} else {
12+
// 之前的原型检查
13+
return left instanceof right;
14+
}
15+
}
16+
17+
function _classCallCheck(instance, Constructor) {
18+
// 原型检测
19+
if (!_instanceof(instance, Constructor)) {
20+
throw new TypeError("Cannot call a class as a function");
21+
}
22+
}
23+
24+
function _defineProperties(target, props) {
25+
// 遍历属性,定义属性
26+
for (var i = 0; i < props.length; i++) {
27+
28+
var descriptor = props[i];
29+
30+
descriptor.enumerable = descriptor.enumerable || false;
31+
32+
descriptor.configurable = true;
33+
if ("value" in descriptor) descriptor.writable = true;
34+
Object.defineProperty(target, descriptor.key, descriptor);
35+
}
36+
}
37+
38+
function _createClass(Constructor, protoProps, staticProps) {
39+
/* 原型链集成 */
40+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
41+
/* 自有属性继承集成 */
42+
if (staticProps) _defineProperties(Constructor, staticProps);
43+
return Constructor;
44+
}
45+
46+
function _defineProperty(obj, key, value) {
47+
if (key in obj) {
48+
/* 防止 writable 为false */
49+
Object.defineProperty(obj, key, {
50+
value: value,
51+
enumerable: true,
52+
configurable: true,
53+
writable: true
54+
});
55+
} else {
56+
obj[key] = value;
57+
}
58+
return obj;
59+
}
60+
61+
var A =
62+
/*#__PURE__*/
63+
function () {
64+
function A() {
65+
_classCallCheck(this, A);
66+
67+
_defineProperty(this, "c", 2);
68+
}
69+
70+
_createClass(A, [{
71+
key: "construtor",
72+
value: function construtor() {
73+
this.a = 22;
74+
}
75+
}, {
76+
key: "foo",
77+
value: function foo() {
78+
return this.c;
79+
}
80+
}]);
81+
82+
return A;
83+
}();

0 commit comments

Comments
 (0)