diff --git a/.DS_Store b/.DS_Store index 6e66fe0..23c078e 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git "a/leetcode and mianshi/\345\240\206\346\240\210.js" "b/leetcode and mianshi/\345\240\206\346\240\210.js" new file mode 100644 index 0000000..5ab8f97 --- /dev/null +++ "b/leetcode and mianshi/\345\240\206\346\240\210.js" @@ -0,0 +1,9 @@ +let a = {n:10} +let b = a +b.m = b = {n: 20} +console.log(a) +console.log(b) + +//https://www.tuicool.com/articles/vUFbeu + +// https://www.tuicool.com/articles/eaeUZvy \ No newline at end of file diff --git "a/\345\217\221\345\270\203\350\256\242\351\230\205\346\250\241\345\274\217.js" "b/\345\217\221\345\270\203\350\256\242\351\230\205\346\250\241\345\274\217.js" new file mode 100644 index 0000000..2544030 --- /dev/null +++ "b/\345\217\221\345\270\203\350\256\242\351\230\205\346\250\241\345\274\217.js" @@ -0,0 +1,57 @@ +class EventBus { + constructor() { + this.events = {} + } + + on(key,cb) { + if(!this.events[key]) { + this.events[key] = [] + } + this.events[key].push(cb) + } + + emit(key,...args) { + this.events[key] && this.events[key].forEach(cb => { + // cb.call(this,...args) + console.log('emit') + // console.log(cb()) + // cb(...args) + cb.call(this,...args) + }) + } + + off(key, cb) { + let index = this.events[key]&&this.events[key].findIndex(item => cb === item) + if(index !== -1) { + this.events[key].splice(index,1) + } + } +} + + +let ev = new EventBus() + +function f1(m,n) { + console.log('f1',m,n) +} + +function f2(a,b) { + console.log('f2', a,b) +} +function f3() { + console.log('f3') +} +function f4() { + console.log('f4') +} +ev.on('f1',f1) +ev.on('f2',f2) +ev.on('f3',f3) +ev.on('f4',f4) +// console.log(ev.events) +ev.emit('f1',1,2) +ev.emit('f2',f2) +ev.emit('f3',f3) +ev.emit('f4',f4) +ev.off('f1',f1) +ev.emit('f3',f3) \ No newline at end of file diff --git "a/\346\225\260\347\273\204\344\270\200\344\272\233\345\270\270\347\224\250\347\232\204\346\226\271\346\263\225\345\256\236\347\216\260.js" "b/\346\225\260\347\273\204\344\270\200\344\272\233\345\270\270\347\224\250\347\232\204\346\226\271\346\263\225\345\256\236\347\216\260.js" new file mode 100644 index 0000000..e807c83 --- /dev/null +++ "b/\346\225\260\347\273\204\344\270\200\344\272\233\345\270\270\347\224\250\347\232\204\346\226\271\346\263\225\345\256\236\347\216\260.js" @@ -0,0 +1,28 @@ +Array.prototype.push = function() { + for(let i = 0; i< arguments.length; i++) { + this[this.length] = arguments[i] + } + return this.length +} + +Array.prototype.pop = function(){ + let len = this.length + if(len === 0) return + let value = this[this.length - 1] + this.length -= 1 + return value +} + +Array.prototype.shift = function() { + let len = this.length + if(len === 0) return + let value = this[0] + var newArr = [] + for(let i = 1; i { + observer.update() + }) + } + addObserver(observer) { + this.observers.push(observer) + } +} + +class Observer { + constructor(name, subject) { + this.name = name + this.subject = subject + this.subject.addObserver(this) + } + update() { + console.log(`${this.name}update,state:${this.subject.getState()}`) + } +} + +let sub = new Subject() +let o1 = new Observer('o1',sub) +let o2 = new Observer('o2',sub) + +sub.setState('更新') + +//一对多,多个观察者对应一个主题对象,当这个主题对象的状态更新的时候,就会通知所有的观察者对象 +// vue的响应式,Dom事件 \ No newline at end of file diff --git "a/\350\256\276\350\256\241\346\250\241\345\274\217\344\271\213\345\215\225\344\276\213\346\250\241\345\274\217.js" "b/\350\256\276\350\256\241\346\250\241\345\274\217\344\271\213\345\215\225\344\276\213\346\250\241\345\274\217.js" new file mode 100644 index 0000000..58dd910 --- /dev/null +++ "b/\350\256\276\350\256\241\346\250\241\345\274\217\344\271\213\345\215\225\344\276\213\346\250\241\345\274\217.js" @@ -0,0 +1,41 @@ +class LoginForm { + constructor() { + this.state = 'hide' + } + show() { + if(this.state === 'show') { + alert('已经显示') + return + } + this.state = 'show' + console.log('登录框显示成功') + } + + hide() { + if(this.state === 'hide') { + alert('已经隐藏') + return + } + this.state = 'hide' + console.log('登录框隐藏成功') + } +} +LoginForm.getInstance = (function() { + let instance + return function() { + if(!instance) { + instance = new LoginForm() + } + return instance + } +})() +let obj1 = LoginForm.getInstance() +obj1.show() +let obj2 = LoginForm.getInstance() +obj2.hide() + +console.log(obj1.state,obj2.state) + +console.log(obj1 === obj2) + +// vuex redux的store \ No newline at end of file