Skip to content

Commit 24982aa

Browse files
committed
添加装饰器模式的例子
1 parent 1f28e4f commit 24982aa

File tree

10,094 files changed

+785623
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

10,094 files changed

+785623
-0
lines changed

babel/decorator/.babelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
presets: [
3+
'es2015', 'stage-0', 'react'
4+
],
5+
plugins: [
6+
"transform-decorators-legacy"
7+
]
8+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"use strict";
2+
3+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4+
5+
var _desc, _value, _class;
6+
7+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8+
9+
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
10+
var desc = {};
11+
Object['ke' + 'ys'](descriptor).forEach(function (key) {
12+
desc[key] = descriptor[key];
13+
});
14+
desc.enumerable = !!desc.enumerable;
15+
desc.configurable = !!desc.configurable;
16+
17+
if ('value' in desc || desc.initializer) {
18+
desc.writable = true;
19+
}
20+
21+
desc = decorators.slice().reverse().reduce(function (desc, decorator) {
22+
return decorator(target, property, desc) || desc;
23+
}, desc);
24+
25+
if (context && desc.initializer !== void 0) {
26+
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
27+
desc.initializer = undefined;
28+
}
29+
30+
if (desc.initializer === void 0) {
31+
Object['define' + 'Property'](target, property, desc);
32+
desc = null;
33+
}
34+
35+
return desc;
36+
}
37+
38+
function decorateArmour(target, key, descriptor) {
39+
console.log("target====", target.constructor);
40+
//防御力:undefined,攻击力:undefined,血量:undefined
41+
console.log("key====",key);
42+
//init
43+
console.log("descriptor====",descriptor);
44+
// { value: [Function: init],
45+
// writable: true,
46+
// enumerable: false,
47+
// configurable: true }
48+
var method = descriptor.value;
49+
var moreDef = 100;
50+
var ret = void 0;
51+
descriptor.value = function () {
52+
console.log('arguments====',arguments);
53+
// { '0': 2, '1': 3, '2': 3 }
54+
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
55+
args[_key] = arguments[_key];
56+
}
57+
args[0] += moreDef;
58+
//操作数组中的第一个元素
59+
ret = method.apply(target, args);
60+
//执行以前的函数并传入新的参数就可以了
61+
return ret;
62+
};
63+
return descriptor;
64+
}
65+
66+
var Man = (_class = function () {
67+
function Man() {
68+
var def = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2;
69+
var atk = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
70+
var hp = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 3;
71+
72+
_classCallCheck(this, Man);
73+
74+
this.init(def, atk, hp);
75+
}
76+
77+
_createClass(Man, [{
78+
key: "init",
79+
value: function init(def, atk, hp) {
80+
this.def = def; // 防御值
81+
this.atk = atk; // 攻击力
82+
this.hp = hp; // 血量
83+
}
84+
}, {
85+
key: "toString",
86+
value: function toString() {
87+
return "\u9632\u5FA1\u529B:" + this.def + ",\u653B\u51FB\u529B:" + this.atk + ",\u8840\u91CF:" + this.hp;
88+
}
89+
}]);
90+
91+
return Man;
92+
}(), (_applyDecoratedDescriptor(_class.prototype, "init", [decorateArmour], Object.getOwnPropertyDescriptor(_class.prototype, "init"), _class.prototype)), _class);
93+
94+
95+
var tony = new Man();
96+
97+
console.log("\u5F53\u524D\u72B6\u6001 ===> " + tony);
98+
// 输出:当前状态 ===> 防御力:102,攻击力:3,血量:3
99+

babel/decorator/decorator1.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function decorateArmour(target, key, descriptor) {
2+
const method = descriptor.value;
3+
//这里的method指的是init方法
4+
let moreDef = 100;
5+
let ret;
6+
descriptor.value = (...args)=>{
7+
args[0] += moreDef;
8+
ret = method.apply(target, args);
9+
return ret;
10+
}
11+
return descriptor;
12+
}
13+
14+
class Man{
15+
constructor(def = 2,atk = 3,hp = 3){
16+
this.init(def,atk,hp);
17+
}
18+
19+
@decorateArmour
20+
init(def,atk,hp){
21+
this.def = def; // 防御值
22+
this.atk = atk; // 攻击力
23+
this.hp = hp; // 血量
24+
}
25+
toString(){
26+
return `防御力:${this.def},攻击力:${this.atk},血量:${this.hp}`;
27+
}
28+
}
29+
30+
var tony = new Man();
31+
32+
console.log(`当前状态 ===> ${tony}`);
33+
// 输出:当前状态 ===> 防御力:102,攻击力:3,血量:3
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
'use strict';
2+
3+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4+
5+
var _dec, _class, _desc, _value, _class2;
6+
7+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8+
9+
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
10+
var desc = {};
11+
Object['ke' + 'ys'](descriptor).forEach(function (key) {
12+
desc[key] = descriptor[key];
13+
});
14+
desc.enumerable = !!desc.enumerable;
15+
desc.configurable = !!desc.configurable;
16+
17+
if ('value' in desc || desc.initializer) {
18+
desc.writable = true;
19+
}
20+
21+
desc = decorators.slice().reverse().reduce(function (desc, decorator) {
22+
return decorator(target, property, desc) || desc;
23+
}, desc);
24+
25+
if (context && desc.initializer !== void 0) {
26+
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
27+
desc.initializer = undefined;
28+
}
29+
30+
if (desc.initializer === void 0) {
31+
Object['define' + 'Property'](target, property, desc);
32+
desc = null;
33+
}
34+
35+
return desc;
36+
}
37+
38+
function decorateArmour(target, key, descriptor) {
39+
var method = descriptor.value;
40+
var moreDef = 100;
41+
var ret = void 0;
42+
descriptor.value = function () {
43+
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
44+
args[_key] = arguments[_key];
45+
}
46+
47+
args[0] += moreDef;
48+
ret = method.apply(target, args);
49+
return ret;
50+
};
51+
return descriptor;
52+
}
53+
54+
function addFly(canFly) {
55+
return function (target) {
56+
//使用装饰器对类进行装饰,那么我们的target就是类本身
57+
target.canFly = canFly;
58+
var extra = canFly ? '(技能加成:飞行能力)' : '';
59+
var method = target.prototype.toString;
60+
//获取类的prototype.toString方法,并且对该方法进行装饰
61+
target.prototype.toString = function () {
62+
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
63+
args[_key2] = arguments[_key2];
64+
}
65+
66+
return method.apply(target.prototype, args) + extra;
67+
};
68+
return target;
69+
};
70+
}
71+
72+
var Man = (_dec = addFly(true), _dec(_class = (_class2 = function () {
73+
function Man() {
74+
var def = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2;
75+
var atk = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
76+
var hp = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 3;
77+
78+
_classCallCheck(this, Man);
79+
80+
this.init(def, atk, hp);
81+
}
82+
83+
_createClass(Man, [{
84+
key: 'init',
85+
value: function init(def, atk, hp) {
86+
this.def = def;
87+
// 防御值
88+
this.atk = atk;
89+
// 攻击力
90+
this.hp = hp;
91+
// 血量
92+
}
93+
}, {
94+
key: 'toString',
95+
value: function toString() {
96+
return '\u9632\u5FA1\u529B:' + this.def + ',\u653B\u51FB\u529B:' + this.atk + ',\u8840\u91CF:' + this.hp;
97+
}
98+
}]);
99+
100+
return Man;
101+
}(), (_applyDecoratedDescriptor(_class2.prototype, 'init', [decorateArmour], Object.getOwnPropertyDescriptor(_class2.prototype, 'init'), _class2.prototype)), _class2)) || _class);
102+
103+
var tony = new Man();
104+
console.log('\u5F53\u524D\u72B6\u6001 ===> ' + tony);
105+

babel/decorator/decorator2.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
function decorateArmour(target, key, descriptor) {
2+
const method = descriptor.value;
3+
let moreDef = 100;
4+
let ret;
5+
descriptor.value = (...args)=>{
6+
args[0] += moreDef;
7+
ret = method.apply(target, args);
8+
return ret;
9+
}
10+
return descriptor;
11+
}
12+
13+
14+
function addFly(canFly){
15+
return function(target){
16+
//使用装饰器对类进行装饰,那么我们的target就是类本身
17+
target.canFly = canFly;
18+
let extra = canFly ? '(技能加成:飞行能力)' : '';
19+
let method = target.prototype.toString;
20+
//获取类的prototype.toString方法,并且对该方法进行装饰
21+
target.prototype.toString = (...args)=>{
22+
return method.apply(target.prototype,args) + extra;
23+
}
24+
return target;
25+
}
26+
}
27+
28+
@addFly(true)
29+
class Man{
30+
constructor(def = 2,atk = 3,hp = 3){
31+
this.init(def,atk,hp);
32+
}
33+
34+
@decorateArmour
35+
init(def,atk,hp){
36+
this.def = def;
37+
// 防御值
38+
this.atk = atk;
39+
// 攻击力
40+
this.hp = hp;
41+
// 血量
42+
}
43+
toString(){
44+
return `防御力:${this.def},攻击力:${this.atk},血量:${this.hp}`;
45+
}
46+
}
47+
var tony = new Man();
48+
console.log(`当前状态 ===> ${tony}`);

babel/decorator/index.compiled.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Man {
2+
constructor(def = 2, atk = 3, hp = 3) {
3+
this.init(def, atk, hp);
4+
}
5+
6+
init(def, atk, hp) {
7+
this.def = def; // 防御值
8+
this.atk = atk; // 攻击力
9+
this.hp = hp; // 血量
10+
}
11+
toString() {
12+
return `防御力:${this.def},攻击力:${this.atk},血量:${this.hp}`;
13+
}
14+
}
15+
16+
var tony = new Man();
17+
18+
console.log(`当前状态 ===> ${tony}`);
19+

babel/decorator/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Man{
2+
constructor(def = 2,atk = 3,hp = 3){
3+
this.init(def,atk,hp);
4+
}
5+
6+
init(def,atk,hp){
7+
this.def = def; // 防御值
8+
this.atk = atk; // 攻击力
9+
this.hp = hp; // 血量
10+
}
11+
toString(){
12+
return `防御力:${this.def},攻击力:${this.atk},血量:${this.hp}`;
13+
}
14+
}
15+
16+
var tony = new Man();
17+
18+
console.log(`当前状态 ===> ${tony}`);

babel/decorator/node_modules/.bin/babylon

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

babel/decorator/node_modules/.bin/jsesc

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

babel/decorator/node_modules/.bin/loose-envify

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

babel/decorator/node_modules/.bin/regjsparser

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

babel/decorator/node_modules/ansi-regex/index.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

babel/decorator/node_modules/ansi-regex/license

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)