Skip to content

Commit

Permalink
learn
Browse files Browse the repository at this point in the history
  • Loading branch information
abstain23 committed Jul 15, 2020
1 parent 2a72f61 commit fc302fe
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 0 deletions.
Empty file.
20 changes: 20 additions & 0 deletions 数据结构和算法/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Queue {
constructor() {
this.queue = []
}
enQueue(item) {
this.queue.push(item)
}
deQueue() {
return this.queue.shift()
}
getHeader() {
return this.queue[0]
}
getLength() {
return this.queue.length
}
isEmpty() {
return this.getLength() === 0
}
}
54 changes: 54 additions & 0 deletions 数据结构和算法/stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class Stack {
constructor() {
this.stack = []
}
push(item) {
this.stack.push(item)
}
pop() {
this.stack.pop()
}
peek() {
return this.stack[this.getLength() - 1]
}

getLength() {
return this.stack.length
}
isEmpty() {
return this.getLength() === 0
}
}

/**
* 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
*/

var isValid = function (s) {
const map = {
'(': -1,
')': 1,
'[': -2,
']': 2,
'{': -3,
'}': 3
}
const len = s.length
const stack = []
for (let i = 0; i < len; i++) {
if (map[s[i]] < 0) {
stack.push(s[i])
} else {
let last = stack.pop()
if(map[last] + map[s[i]] !== 0) return false
}
}
if (stack.length > 0) return false
return true
}
1 change: 1 addition & 0 deletions 设计模式/代理模式.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// 事件代理
File renamed without changes.
16 changes: 16 additions & 0 deletions 设计模式/工厂模式.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Person {
constructor(name) {
this.name = name
}
sayName() {
console.log(this.name)
}
}

class Factory {
static create() {
return new Person('cc')
}
}

// 工厂模式起到的作用就是替我们隐藏了一堆复杂的处理逻辑,我们只需要调用一个方法或者函数就能实现功能
16 changes: 16 additions & 0 deletions 设计模式/装饰器模式.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// es7 装饰器语法

function readonly(target, key, descriptor) {
descriptor.writable = false
return descriptor
}

class Test {
@readonly
name = 'cc'
}

let t = new Test()
t.name = 'xxx'

// 例如react中的 react-redux中的 connect
File renamed without changes.
19 changes: 19 additions & 0 deletions 设计模式/适配器模式.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Plug {
getName() {
return '港版插头'
}
}

class Target {
constructor() {
this.plug = new Plug()
}
getName() {
return this.plug.getName() + ' 适配器转二脚插头'
}
}

let target = new Target()
target.getName() // 港版插头 适配器转二脚插头

// 如vue的computed,对传进来的value进行包装转换。

0 comments on commit fc302fe

Please sign in to comment.