├── .DS_Store
├── .babelrc
├── .gitignore
├── .md
├── 1.md
├── README.md
├── package-lock.json
├── package.json
├── public
├── bundle.html
├── bundle.js
├── bundle.js.map
└── 双向数据绑定.html
├── rollup.config.js
├── src
├── .DS_Store
├── React
│ ├── index.html
│ ├── index.js
│ ├── public
│ │ ├── bundle.js
│ │ └── bundle.js.map
│ └── rollup.config.js
├── React技术揭秘
│ ├── babel
│ │ └── babel.min.js
│ ├── react
│ │ ├── react-dom.development.js
│ │ └── react.development.js
│ └── reactDemo.html
├── assets
│ └── img
│ │ └── loading.gif
├── main.js
├── 手写系列
│ ├── .DS_Store
│ ├── Promise
│ │ ├── testPromise.js
│ │ ├── 基本用法.js
│ │ ├── 多次调用then.js
│ │ ├── 异步事件.js
│ │ └── 链式调用then.js
│ ├── React diff
│ │ └── index.js
│ ├── React
│ │ ├── .DS_Store
│ │ ├── babel
│ │ │ └── babel.min.js
│ │ ├── react
│ │ │ ├── react-dom.development.js
│ │ │ └── react.development.js
│ │ ├── reactDemo.html
│ │ ├── reactDemo10.html
│ │ ├── reactDemo11.html
│ │ ├── reactDemo12.html
│ │ ├── reactDemo2.html
│ │ ├── reactDemo3.html
│ │ ├── reactDemo4.html
│ │ ├── reactDemo5.html
│ │ ├── reactDemo6.html
│ │ ├── reactDemo7.html
│ │ ├── reactDemo8.html
│ │ └── reactDemo9.html
│ ├── 虚拟DOM
│ │ ├── READ.md
│ │ ├── index.html
│ │ ├── public
│ │ │ ├── bundle.js
│ │ │ └── bundle.js.map
│ │ ├── rollup.config.js
│ │ └── src
│ │ │ ├── index.js
│ │ │ ├── lib-test
│ │ │ ├── const.js
│ │ │ ├── diff.js
│ │ │ ├── patch.js
│ │ │ └── util.js
│ │ │ └── lib
│ │ │ ├── diff.js
│ │ │ ├── element.js
│ │ │ ├── patch.js
│ │ │ └── util.js
│ └── 虚拟DOM实例
│ │ └── 分页
│ │ ├── 真实DOM
│ │ ├── index.html
│ │ ├── index.js
│ │ ├── public
│ │ │ ├── bundle.js
│ │ │ └── bundle.js.map
│ │ └── rollup.config.js
│ │ └── 虚拟DOM
│ │ ├── 1.json
│ │ ├── index.html
│ │ ├── index.js
│ │ ├── public
│ │ ├── bundle.js
│ │ └── bundle.js.map
│ │ └── rollup.config.js
└── 设计模式
│ ├── .DS_Store
│ ├── 代理模式
│ ├── .DS_Store
│ ├── 分页
│ │ └── 缓存代理
│ │ │ └── index.js
│ ├── 合并HTTP请求
│ │ └── 虚拟代理
│ │ │ ├── index.html
│ │ │ └── index.js
│ ├── 图片预加载
│ │ ├── .DS_Store
│ │ ├── 普通
│ │ │ ├── .DS_Store
│ │ │ ├── index.html
│ │ │ └── index.js
│ │ └── 虚拟代理
│ │ │ ├── .DS_Store
│ │ │ ├── index.html
│ │ │ └── index.js
│ └── 小明送花给小白
│ │ ├── .DS_Store
│ │ ├── 代理模式
│ │ ├── .DS_Store
│ │ ├── index.html
│ │ └── index.js
│ │ └── 普通
│ │ ├── .DS_Store
│ │ ├── index.html
│ │ └── index.js
│ ├── 单例模式
│ ├── .DS_Store
│ ├── 代理实现单例模式
│ │ ├── index.html
│ │ └── index.js
│ ├── 实现单例模式
│ │ ├── .DS_Store
│ │ ├── index.html
│ │ └── 实现单例模式.js
│ ├── 登录浮窗
│ │ ├── .DS_Store
│ │ ├── 单例模式
│ │ │ ├── index.html
│ │ │ └── index.js
│ │ ├── 普通
│ │ │ ├── .DS_Store
│ │ │ ├── index.html
│ │ │ ├── index.js
│ │ │ └── index2.js
│ │ └── 普通2
│ │ │ ├── .DS_Store
│ │ │ ├── index.html
│ │ │ └── index.js
│ ├── 透明的单例模式
│ │ ├── .DS_Store
│ │ ├── index.html
│ │ └── 透明的单例模式.js
│ └── 通用的惰性单例
│ │ ├── index.html
│ │ └── index.js
│ ├── 策略模式
│ ├── .DS_Store
│ ├── 什么是快乐星球,什么是策略模式.md
│ └── 表单校验
│ │ ├── .DS_Store
│ │ ├── 普通
│ │ ├── .DS_Store
│ │ ├── index.html
│ │ └── index.js
│ │ └── 策略
│ │ ├── .DS_Store
│ │ ├── index.html
│ │ ├── index.js
│ │ └── index1.js
│ ├── 职责链模式
│ └── 预售订单
│ │ ├── index.html
│ │ └── index.js
│ ├── 装饰器模式
│ ├── ajax参数.js
│ ├── index.js
│ ├── 数据上报.js
│ └── 鸭子.js
│ └── 观察者模式
│ ├── .DS_Store
│ ├── 双向数据绑定
│ ├── .DS_Store
│ ├── 完整版
│ │ ├── 2-way-binding.html
│ │ ├── tbinding.js
│ │ └── user.js
│ └── 简单版
│ │ └── index.js
│ └── 观察者模式函数
│ └── index.js
└── 白帽子讲web安全
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jiaozitang/web-learn-note/d9fcfbc68277332a9be0ac85d2a137f51be14060/.DS_Store
--------------------------------------------------------------------------------
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["@babel/preset-env"],
3 | "plugins": ["@babel/plugin-transform-runtime"]
4 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /node_modules
2 | .history
3 | package-lock.json
4 |
5 | index2.html
--------------------------------------------------------------------------------
/.md:
--------------------------------------------------------------------------------
1 | # 一、什么是职责链模式
2 |
3 | 职责链模式就是当一个对象 `a`,有多种可能的请求对象 `b`、`c`、`d`、`e` 时,我们为 `b`、`c`、`d`、`e` 分别定义一个职责,组成一条职责链,这样 `a` 只需要找到 `b` 发起请求,然后沿着职责链继续请求,直到找到一个对象来处理 `a`。
4 |
5 | 女孩子们都喜欢结伴吃饭,我现在要找一个人一起吃饭,代码如下:
6 |
7 | > 嗯.....女程序员确实是这样,吃个饭也要写代码发请求的。
8 |
9 | ```javascript
10 | const [ astrid, brooke, calliope ] = [{
11 | name: 'astrid',
12 | requirement: '我要吃湘菜'
13 | },{
14 | name: 'brooke',
15 | requirement: '我要找10个人一起吃饭'
16 | },{
17 | name: 'calliope',
18 | requirement: '我要和男朋友一起吃饭'
19 | }]
20 |
21 | // 是否满足
22 | function isSatisfyAstrid (user) {
23 | // ... 省略...
24 | }
25 |
26 | // 是否满足
27 | function isSatisfyBrooke (user) {
28 | // ... 省略...
29 | }
30 |
31 | // 是否满足
32 | function isSatisfyCalliope (user) {
33 | // ... 省略...
34 | }
35 |
36 | function eatDinner () {
37 | if (isSatisfyAstrid()) {
38 | console.log(`我可以和 astrid 一起吃晚饭啦`)
39 | } else if (isSatisfyBrooke()) {
40 | console.log(`我可以和 brooke 一起吃晚饭啦`)
41 | } else if (isSatisfyCalliope()) {
42 | console.log(`我可以和 calliope 一起吃晚饭啦`)
43 | } else {
44 | console.log(`哎呀,我要一个人吃晚饭啦`)
45 | }
46 | }
47 | ```
48 |
49 | 由于 `astrid`、`brooke`、`calliope` 吃晚饭的要求不同,我需要一个个去问,直到找到答应和我一起吃晚饭的人。
50 |
51 | 在这里,我假设 `astrid` 的要求是要吃湘菜,`brooke` 的要求是要找 10 个人凑一桌一起吃,`calliope` 的要求是只想和男朋友一起吃饭。
52 |
53 | 用上述 `if-else` 太死板了,假如我又多了个朋友 `davi`,我必须再次修改 `eatDinner` 方法,违反了开放-封闭原则,不易于维护。
54 |
55 | 使用职责链来优化上述代码,代码如下:
56 |
57 | ```javascript
58 | // 给每个人定义一个职责
59 | const chainOrderA = new Chain(isSatisfyAstrid)
60 | const chainOrderB = new Chain(isSatisfyBrooke)
61 | const chainOrderC = new Chain(isSatisfyCalliope)
62 |
63 | // 设置一下职责链的顺序
64 | chainOrderA.setNextSuccessor(chainOrderB)
65 | chainOrderB.setNextSuccessor(chainOrderC)
66 |
67 | // 发起请求,这时我只需要向职责链上的第一个人请求
68 |
69 |
70 |
71 | ```
72 |
73 |
74 |
75 | # 二、实际场景
76 |
77 | # 三、小结
--------------------------------------------------------------------------------
/1.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jiaozitang/web-learn-note/d9fcfbc68277332a9be0ac85d2a137f51be14060/1.md
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # web-learn-note
2 |
3 | ## 快速开始
4 |
5 | 1. git clone
6 |
7 | ```
8 | git clone https://github.com/jiaozitang/web-learn-note
9 | ```
10 |
11 | 2. 安装依赖
12 |
13 | ```
14 | npm i
15 |
16 | ```
17 |
18 | 3. 本地运行
19 | ```
20 | npm run dev
21 | ```
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "web-learn-note",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1",
8 | "dev": "npm run watch",
9 | "push": "git add . && git commit -m '手写系列'",
10 | "watch": "rollup -c -w",
11 | "test:promise": "promises-aplus-tests ./src/手写系列/Promise/testPromise"
12 | },
13 | "repository": {
14 | "type": "git",
15 | "url": "git+https://github.com/jiaozitang/web-learn-note.git"
16 | },
17 | "author": "",
18 | "license": "ISC",
19 | "bugs": {
20 | "url": "https://github.com/jiaozitang/web-learn-note/issues"
21 | },
22 | "homepage": "https://github.com/jiaozitang/web-learn-note#readme",
23 | "devDependencies": {
24 | "@babel/core": "^7.14.3",
25 | "@babel/plugin-transform-runtime": "^7.14.3",
26 | "@babel/preset-env": "^7.14.4",
27 | "@babel/preset-react": "^7.14.5",
28 | "@rollup/plugin-babel": "^5.3.0",
29 | "@rollup/plugin-commonjs": "^19.0.0",
30 | "@rollup/plugin-node-resolve": "^13.0.0",
31 | "deepmerge": "^4.2.2",
32 | "is-module": "^1.0.0",
33 | "promises-aplus-tests": "^2.1.2",
34 | "rollup": "^2.50.5",
35 | "rollup-plugin-terser": "^7.0.2"
36 | },
37 | "dependencies": {
38 | "list-diff2": "^0.1.4",
39 | "lodash": "^4.17.21",
40 | "simple-virtual-dom": "^0.1.10"
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/public/bundle.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Document
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/public/bundle.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | 'use strict';
3 |
4 | function _typeof(obj) {
5 | "@babel/helpers - typeof";
6 |
7 | if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
8 | _typeof = function _typeof(obj) {
9 | return typeof obj;
10 | };
11 | } else {
12 | _typeof = function _typeof(obj) {
13 | return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
14 | };
15 | }
16 |
17 | return _typeof(obj);
18 | }
19 |
20 | function _classCallCheck(instance, Constructor) {
21 | if (!(instance instanceof Constructor)) {
22 | throw new TypeError("Cannot call a class as a function");
23 | }
24 | }
25 |
26 | function _defineProperties(target, props) {
27 | for (var i = 0; i < props.length; i++) {
28 | var descriptor = props[i];
29 | descriptor.enumerable = descriptor.enumerable || false;
30 | descriptor.configurable = true;
31 | if ("value" in descriptor) descriptor.writable = true;
32 | Object.defineProperty(target, descriptor.key, descriptor);
33 | }
34 | }
35 |
36 | function _createClass(Constructor, protoProps, staticProps) {
37 | if (protoProps) _defineProperties(Constructor.prototype, protoProps);
38 | if (staticProps) _defineProperties(Constructor, staticProps);
39 | return Constructor;
40 | }
41 |
42 | function _defineProperty(obj, key, value) {
43 | if (key in obj) {
44 | Object.defineProperty(obj, key, {
45 | value: value,
46 | enumerable: true,
47 | configurable: true,
48 | writable: true
49 | });
50 | } else {
51 | obj[key] = value;
52 | }
53 |
54 | return obj;
55 | }
56 |
57 | var STATUS = {
58 | PENDING: 'pending',
59 | FULFILLED: 'fulfilled',
60 | REJECTED: 'rejected'
61 | };
62 |
63 | var MyPromise = /*#__PURE__*/function () {
64 | function MyPromise(executor) {
65 | var _this = this;
66 |
67 | _classCallCheck(this, MyPromise);
68 |
69 | _defineProperty(this, "onFulfilledCallbacks", []);
70 |
71 | _defineProperty(this, "onRejectedCallbacks", []);
72 |
73 | _defineProperty(this, "status", STATUS.PENDING);
74 |
75 | _defineProperty(this, "value", null);
76 |
77 | _defineProperty(this, "resolve", null);
78 |
79 | _defineProperty(this, "resolve", function (value) {
80 | if (_this.status === STATUS.PENDING) {
81 | _this.status = STATUS.FULFILLED;
82 | _this.value = value;
83 |
84 | while (_this.onFulfilledCallbacks.length) {
85 | _this.onFulfilledCallbacks.shift()(value);
86 | }
87 | }
88 | });
89 |
90 | _defineProperty(this, "reject", function (value) {
91 | if (_this.status === STATUS.PENDING) {
92 | _this.status = STATUS.REJECTED;
93 | _this.reason = value;
94 |
95 | while (_this.onRejectedCallbacks.length) {
96 | _this.onRejectedCallbacks.shift()(value);
97 | }
98 | }
99 | });
100 |
101 | // 执行器
102 | try {
103 | executor(this.resolve, this.reject);
104 | } catch (error) {
105 | this.reject(error);
106 | }
107 | } // 成功回调
108 |
109 |
110 | _createClass(MyPromise, [{
111 | key: "then",
112 | value: function then(onFulfilled, onRejected) {
113 | var _this2 = this;
114 |
115 | var realOnFulfilled = typeof onFulfilled === 'function' ? onFulfilled : function (value) {
116 | return value;
117 | };
118 | var realOnRejected = typeof onRejected === 'function' ? onRejected : function (error) {
119 | throw error;
120 | };
121 | var promise2 = new MyPromise(function (resolve, reject) {
122 | var fulfilledMicrotask = function fulfilledMicrotask() {
123 | // 创建一个微任务等待 promise2 完成初始化
124 | queueMicrotask(function () {
125 | try {
126 | // 获取成功回调函数的执行结果
127 | var x = realOnFulfilled(_this2.value); // 传入 resolvePromise 集中处理
128 |
129 | resolvePromise(promise2, x, resolve, reject);
130 | } catch (error) {
131 | reject(error);
132 | }
133 | });
134 | };
135 |
136 | var rejectedMicrotask = function rejectedMicrotask() {
137 | // 创建一个微任务等待 promise2 完成初始化
138 | queueMicrotask(function () {
139 | try {
140 | // 调用失败回调,并且把原因返回
141 | var x = realOnRejected(_this2.reason); // 传入 resolvePromise 集中处理
142 |
143 | resolvePromise(promise2, x, resolve, reject);
144 | } catch (error) {
145 | reject(error);
146 | }
147 | });
148 | };
149 |
150 | if (_this2.status === STATUS.PENDING) {
151 | _this2.onFulfilledCallbacks.push(fulfilledMicrotask);
152 |
153 | _this2.onRejectedCallbacks.push(rejectedMicrotask);
154 | } else if (_this2.status === STATUS.FULFILLED) {
155 | fulfilledMicrotask();
156 | } else if (_this2.status === STATUS.REJECTED) {
157 | rejectedMicrotask();
158 | }
159 | });
160 | return promise2;
161 | }
162 | }]);
163 |
164 | return MyPromise;
165 | }();
166 |
167 | function resolvePromise(promise, x, resolve, reject) {
168 | // 如果 promise === x, 执行 reject,错误原因为 TypeError
169 | if (promise === x) {
170 | return reject(new TypeError('The promise and the return value are the same'));
171 | } // 如果 x 是对象或函数
172 |
173 |
174 | if (_typeof(x) === 'object' || typeof x === 'function') {
175 | if (x === null) {
176 | return resolve(x);
177 | }
178 |
179 | var then;
180 |
181 | try {
182 | then = x.then;
183 | } catch (error) {
184 | return reject(error);
185 | } // 如果 x.then 是函数
186 |
187 |
188 | if (typeof then === 'function') {
189 | var called = false;
190 |
191 | try {
192 | then.call(x, function (y) {
193 | // resolve的结果依旧是promise 那就继续解析
194 | if (called) return;
195 | called = true;
196 | resolvePromise(promise, y, resolve, reject);
197 | }, function (err) {
198 | if (called) return;
199 | called = true;
200 | reject(err); // 失败了
201 | });
202 | } catch (error) {
203 | if (called) return;
204 | reject(error);
205 | }
206 | } else {
207 | // 如果 x.then 不是函数
208 | resolve(x);
209 | }
210 | } else {
211 | // 如果 x 不是 promise 实例
212 | resolve(x);
213 | }
214 | }
215 |
216 | var mypromise = new MyPromise(function (resolve, reject) {
217 | setTimeout(function () {
218 | return resolve('成功');
219 | }, 1000);
220 | });
221 | var mypromise2 = new MyPromise(function (resolve, reject) {
222 | setTimeout(function () {
223 | return resolve('成功2');
224 | }, 1000);
225 | });
226 | mypromise.then(function (data) {
227 | console.log(data, '1');
228 | return mypromise2;
229 | }).then(function (data) {
230 | console.log(data, '2');
231 | });
232 |
233 | }());
234 | //# sourceMappingURL=bundle.js.map
235 |
--------------------------------------------------------------------------------
/public/bundle.js.map:
--------------------------------------------------------------------------------
1 | {"version":3,"file":"bundle.js","sources":["../node_modules/@babel/runtime/helpers/esm/typeof.js","../node_modules/@babel/runtime/helpers/esm/classCallCheck.js","../node_modules/@babel/runtime/helpers/esm/createClass.js","../node_modules/@babel/runtime/helpers/esm/defineProperty.js","../src/手写系列/Promise/异步事件.js"],"sourcesContent":["export default function _typeof(obj) {\n \"@babel/helpers - typeof\";\n\n if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") {\n _typeof = function _typeof(obj) {\n return typeof obj;\n };\n } else {\n _typeof = function _typeof(obj) {\n return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj;\n };\n }\n\n return _typeof(obj);\n}","export default function _classCallCheck(instance, Constructor) {\n if (!(instance instanceof Constructor)) {\n throw new TypeError(\"Cannot call a class as a function\");\n }\n}","function _defineProperties(target, props) {\n for (var i = 0; i < props.length; i++) {\n var descriptor = props[i];\n descriptor.enumerable = descriptor.enumerable || false;\n descriptor.configurable = true;\n if (\"value\" in descriptor) descriptor.writable = true;\n Object.defineProperty(target, descriptor.key, descriptor);\n }\n}\n\nexport default function _createClass(Constructor, protoProps, staticProps) {\n if (protoProps) _defineProperties(Constructor.prototype, protoProps);\n if (staticProps) _defineProperties(Constructor, staticProps);\n return Constructor;\n}","export default function _defineProperty(obj, key, value) {\n if (key in obj) {\n Object.defineProperty(obj, key, {\n value: value,\n enumerable: true,\n configurable: true,\n writable: true\n });\n } else {\n obj[key] = value;\n }\n\n return obj;\n}","const STATUS = {\n PENDING: 'pending',\n FULFILLED: 'fulfilled',\n REJECTED: 'rejected'\n}\n\nclass MyPromise {\n constructor (executor) {\n // 执行器\n try {\n executor(this.resolve, this.reject)\n } catch (error) {\n this.reject(error)\n }\n }\n\n // 成功回调\n onFulfilledCallbacks = []\n\n // 失败回调\n onRejectedCallbacks = []\n\n // 初始状态\n status = STATUS.PENDING\n \n // 成功返回值\n value = null\n \n // 失败返回值\n resolve = null\n \n // 修改 Promise 状态,并定义成功返回值\n resolve = value => {\n if (this.status === STATUS.PENDING) {\n this.status = STATUS.FULFILLED\n this.value = value\n\n while(this.onFulfilledCallbacks.length) {\n this.onFulfilledCallbacks.shift()(value)\n }\n }\n }\n \n // 修改 Promise 状态,并定义失败返回值\n reject = value => {\n if (this.status === STATUS.PENDING) {\n this.status = STATUS.REJECTED\n this.reason = value\n\n while(this.onRejectedCallbacks.length) {\n this.onRejectedCallbacks.shift()(value)\n }\n }\n }\n\n then (onFulfilled, onRejected) {\n const realOnFulfilled = typeof onFulfilled === 'function' ? onFulfilled : value => value\n const realOnRejected = typeof onRejected === 'function' ? onRejected : error => { throw error }\n \n const promise2 = new MyPromise((resolve, reject) => {\n \n const fulfilledMicrotask = () => {\n // 创建一个微任务等待 promise2 完成初始化\n queueMicrotask(() => {\n try {\n // 获取成功回调函数的执行结果\n const x = realOnFulfilled(this.value);\n // 传入 resolvePromise 集中处理\n resolvePromise(promise2, x, resolve, reject);\n } catch (error) {\n reject(error)\n } \n }) \n }\n \n const rejectedMicrotask = () => { \n // 创建一个微任务等待 promise2 完成初始化\n queueMicrotask(() => {\n try {\n // 调用失败回调,并且把原因返回\n const x = realOnRejected(this.reason);\n // 传入 resolvePromise 集中处理\n resolvePromise(promise2, x, resolve, reject);\n } catch (error) {\n reject(error)\n } \n }) \n }\n\n if (this.status === STATUS.PENDING) {\n this.onFulfilledCallbacks.push(fulfilledMicrotask)\n this.onRejectedCallbacks.push(rejectedMicrotask)\n } else if (this.status === STATUS.FULFILLED) {\n fulfilledMicrotask()\n } else if (this.status === STATUS.REJECTED) {\n rejectedMicrotask()\n }\n }) \n\n return promise2\n }\n}\n\nfunction resolvePromise (promise, x, resolve, reject) {\n // 如果 promise === x, 执行 reject,错误原因为 TypeError\n if (promise === x) {\n return reject(new TypeError('The promise and the return value are the same'))\n }\n\n // 如果 x 是对象或函数\n if (typeof x === 'object' || typeof x === 'function') {\n\n if (x === null) {\n return resolve(x);\n }\n\n let then\n try {\n then = x.then\n } catch (error) {\n return reject(error)\n }\n\n // 如果 x.then 是函数\n if (typeof then === 'function') {\n let called = false;\n try {\n then.call(x, y => {\n // resolve的结果依旧是promise 那就继续解析\n if (called) return;\n called = true;\n resolvePromise(promise, y, resolve, reject);\n }, err => {\n if (called) return;\n called = true;\n reject(err);// 失败了\n })\n } catch (error) {\n if (called) return;\n reject(error)\n }\n } else {\n // 如果 x.then 不是函数\n resolve(x)\n }\n } else {\n // 如果 x 不是 promise 实例\n resolve(x)\n }\n\n}\n\nconst mypromise = new MyPromise((resolve, reject) => {\n setTimeout(() => resolve('成功'), 1000)\n})\n\nconst mypromise2 = new MyPromise((resolve, reject) => {\n setTimeout(() => resolve('成功2'), 1000)\n})\n\nmypromise.then(data => {\n console.log(data, '1')\n return mypromise2 \n}).then(data => {\n console.log(data, '2')\n})"],"names":["_typeof","obj","Symbol","iterator","constructor","prototype","_classCallCheck","instance","Constructor","TypeError","_defineProperties","target","props","i","length","descriptor","enumerable","configurable","writable","Object","defineProperty","key","_createClass","protoProps","staticProps","_defineProperty","value","STATUS","PENDING","FULFILLED","REJECTED","MyPromise","executor","status","onFulfilledCallbacks","shift","reason","onRejectedCallbacks","resolve","reject","error","onFulfilled","onRejected","realOnFulfilled","realOnRejected","promise2","fulfilledMicrotask","queueMicrotask","x","resolvePromise","rejectedMicrotask","push","promise","then","called","call","y","err","mypromise","setTimeout","mypromise2","data","console","log"],"mappings":";;;EAAe,SAASA,OAAT,CAAiBC,GAAjB,EAAsB;EACnC;;EAEA,MAAI,OAAOC,MAAP,KAAkB,UAAlB,IAAgC,OAAOA,MAAM,CAACC,QAAd,KAA2B,QAA/D,EAAyE;EACvEH,IAAAA,OAAO,GAAG,SAASA,OAAT,CAAiBC,GAAjB,EAAsB;EAC9B,aAAO,OAAOA,GAAd;EACD,KAFD;EAGD,GAJD,MAIO;EACLD,IAAAA,OAAO,GAAG,SAASA,OAAT,CAAiBC,GAAjB,EAAsB;EAC9B,aAAOA,GAAG,IAAI,OAAOC,MAAP,KAAkB,UAAzB,IAAuCD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------------------------------------------------------------------------
/public/双向数据绑定.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Document
7 |
8 |
9 | 姓名:
10 | 年龄:
11 |
12 | 输入的姓名是
13 | 输入的年龄是
14 |
15 |
16 |
--------------------------------------------------------------------------------
/rollup.config.js:
--------------------------------------------------------------------------------
1 | import resolve from '@rollup/plugin-node-resolve';
2 | import commonjs from '@rollup/plugin-commonjs';
3 | import { terser } from 'rollup-plugin-terser';
4 | import { babel } from '@rollup/plugin-babel';
5 |
6 | // `npm run build` -> `production` is true
7 | // `npm run dev` -> `production` is false
8 | const production = !process.env.ROLLUP_WATCH;
9 |
10 | export default {
11 | input: 'src/main.js',
12 | output: {
13 | file: 'public/bundle.js',
14 | format: 'iife', // immediately-invoked function expression — suitable for
13 |
14 |
15 |