const search = (nums, target) => {
let i = 0
let j = nums.length - 1
let midIndex = 0
while (i <= j) {
midIndex = Math.floor((i + j) / 2)
const midValue = nums[ midIndex ]
if (midValue === target) {
return midIndex
} else if (midValue < target) {
i = midIndex + 1
} else {
j = midIndex - 1
}
}
return -1
}
console.log(search([-1,0,3,5,9,12], 9))
const create = (prop, props) => {
if (![ 'object', 'function' ].includes(typeof prop)) {
throw new TypeError(`Object prototype may only be an Object or null: ${prop}`)
}
const Ctor = function () {}
Ctor.prototype = prop
const obj = new Ctor()
if (props) {
Object.defineProperties(obj, props)
}
if (prop === null) {
obj.__proto__ = null
}
return obj
}
const person = {
showName () {
console.log(this.name)
}
};
const me = Object.create(person)
const me2 = create(person)
me.name = '前端胖头鱼'
me2.name = '前端胖头鱼'
me.showName()
me2.showName()
const emptyObj = Object.create(null)
const emptyObj2 = create(null)
console.log(emptyObj)
console.log(emptyObj2)
const props = {
foo: {
writable:true,
configurable:true,
value: "hello"
},
bar: {
configurable: false,
get: function() { return 10 },
set: function(value) {
console.log("Setting `o.bar` to", value);
}
}
}
let o = Object.create(Object.prototype, props)
let o2 = create(Object.prototype, props)
o.bar = '前端胖头鱼'
o2.bar = '前端胖头鱼'
console.log(o.foo)
console.log(o.bar)
console.log(o2.foo)
console.log(o2.bar)
class EventEmitter {
constructor () {
this.events = {}
}
on (evt, callback, ctx) {
if (!this.events[ evt ]) {
this.events[ evt ] = []
}
this.events[ evt ].push(callback)
return this
}
emit (evt, ...payload) {
const callbacks = this.events[ evt ]
if (callbacks) {
callbacks.forEach((cb) => cb.apply(this, payload))
}
return this
}
off (evt, callback) {
if (typeof evt === 'undefined') {
delete this.events
} else if (typeof evt === 'string') {
if (typeof callback === 'function') {
this.events[ evt ] = this.events[ evt ].filter((cb) => cb !== callback)
} else {
delete this.events[ evt ]
}
}
return this
}
once (evt, callback, ctx) {
const proxyCallback = (...payload) => {
callback.apply(ctx, payload)
this.off(evt, proxyCallback)
}
this.on(evt, proxyCallback, ctx)
}
}
const e1 = new EventEmitter()
const e1Callback1 = (name, sex) => {
console.log(name, sex, 'evt1---callback1')
}
const e1Callback2 = (name, sex) => {
console.log(name, sex, 'evt1---callback2')
}
const e1Callback3 = (name, sex) => {
console.log(name, sex, 'evt1---callback3')
}
e1.on('evt1', e1Callback1)
e1.on('evt1', e1Callback2)
e1.once('evt1', e1Callback3)
e1.emit('evt1', '前端胖头鱼', 'boy')
console.log('------尝试删除e1Callback1------')
e1.off('evt1', e1Callback1)
e1.emit('evt1', '前端胖头鱼', 'boy')
const getType = (s) => {
const r = Object.prototype.toString.call(s)
return r.replace(/\[object (.*?)\]/, '$1').toLowerCase()
}
console.log(getType())
console.log(getType(null))
console.log(getType(1))
console.log(getType('前端胖头鱼'))
console.log(getType(true))
console.log(getType(Symbol('前端胖头鱼')))
console.log(getType({}))
console.log(getType([]))
var LRUCache = function (capacity) {
this.keys = []
this.cache = {}
this.capacity = capacity
}
LRUCache.prototype.get = function (key) {
if (this.cache[key]) {
remove(this.keys, key)
this.keys.push(key)
return this.cache[key]
}
return -1
}
LRUCache.prototype.put = function (key, value) {
if (this.cache[key]) {
this.cache[key] = value
remove(this.keys, key)
this.keys.push(key)
} else {
this.keys.push(key)
this.cache[key] = value
if (this.keys.length > this.capacity) {
removeCache(this.cache, this.keys, this.keys[0])
}
}
}
function remove(arr, key) {
if (arr.length) {
const index = arr.indexOf(key)
if (index > -1) {
return arr.splice(index, 1)
}
}
}
function removeCache(cache, keys, key) {
cache[key] = null
remove(keys, key)
}
const lRUCache = new LRUCache(2)
console.log(lRUCache.put(1, 1))
console.log(lRUCache.put(2, 2))
console.log(lRUCache.get(1))
console.log(lRUCache.put(3, 3))
console.log(lRUCache.get(2))
console.log(lRUCache.put(4, 4))
console.log(lRUCache.get(1) )
console.log(lRUCache.get(3))
console.log(lRUCache.get(4) )
const search = (nums, target) => {
let i = 0
let j = nums.length - 1
let midIndex = 0
while (i <= j) {
midIndex = Math.floor((i + j) / 2)
const midValue = nums[ midIndex ]
if (midValue === target) {
return midIndex
} else if (midValue < target) {
i = midIndex + 1
} else {
j = midIndex - 1
}
}
return -1
}
console.log(search([-1,0,3,5,9,12], 9))
const create = (prop, props) => {
if (![ 'object', 'function' ].includes(typeof prop)) {
throw new TypeError(`Object prototype may only be an Object or null: ${prop}`)
}
const Ctor = function () {}
Ctor.prototype = prop
const obj = new Ctor()
if (props) {
Object.defineProperties(obj, props)
}
if (prop === null) {
obj.__proto__ = null
}
return obj
}
const person = {
showName () {
console.log(this.name)
}
};
const me = Object.create(person)
const me2 = create(person)
me.name = '前端胖头鱼'
me2.name = '前端胖头鱼'
me.showName()
me2.showName()
const emptyObj = Object.create(null)
const emptyObj2 = create(null)
console.log(emptyObj)
console.log(emptyObj2)
const props = {
foo: {
writable:true,
configurable:true,
value: "hello"
},
bar: {
configurable: false,
get: function() { return 10 },
set: function(value) {
console.log("Setting `o.bar` to", value);
}
}
}
let o = Object.create(Object.prototype, props)
let o2 = create(Object.prototype, props)
o.bar = '前端胖头鱼'
o2.bar = '前端胖头鱼'
console.log(o.foo)
console.log(o.bar)
console.log(o2.foo)
console.log(o2.bar)
class EventEmitter {
constructor () {
this.events = {}
}
on (evt, callback, ctx) {
if (!this.events[ evt ]) {
this.events[ evt ] = []
}
this.events[ evt ].push(callback)
return this
}
emit (evt, ...payload) {
const callbacks = this.events[ evt ]
if (callbacks) {
callbacks.forEach((cb) => cb.apply(this, payload))
}
return this
}
off (evt, callback) {
if (typeof evt === 'undefined') {
delete this.events
} else if (typeof evt === 'string') {
if (typeof callback === 'function') {
this.events[ evt ] = this.events[ evt ].filter((cb) => cb !== callback)
} else {
delete this.events[ evt ]
}
}
return this
}
once (evt, callback, ctx) {
const proxyCallback = (...payload) => {
callback.apply(ctx, payload)
this.off(evt, proxyCallback)
}
this.on(evt, proxyCallback, ctx)
}
}
const e1 = new EventEmitter()
const e1Callback1 = (name, sex) => {
console.log(name, sex, 'evt1---callback1')
}
const e1Callback2 = (name, sex) => {
console.log(name, sex, 'evt1---callback2')
}
const e1Callback3 = (name, sex) => {
console.log(name, sex, 'evt1---callback3')
}
e1.on('evt1', e1Callback1)
e1.on('evt1', e1Callback2)
e1.once('evt1', e1Callback3)
e1.emit('evt1', '前端胖头鱼', 'boy')
console.log('------尝试删除e1Callback1------')
e1.off('evt1', e1Callback1)
e1.emit('evt1', '前端胖头鱼', 'boy')
const getType = (s) => {
const r = Object.prototype.toString.call(s)
return r.replace(/\[object (.*?)\]/, '$1').toLowerCase()
}
console.log(getType())
console.log(getType(null))
console.log(getType(1))
console.log(getType('前端胖头鱼'))
console.log(getType(true))
console.log(getType(Symbol('前端胖头鱼')))
console.log(getType({}))
console.log(getType([]))
var LRUCache = function (capacity) {
this.keys = []
this.cache = {}
this.capacity = capacity
}
LRUCache.prototype.get = function (key) {
if (this.cache[key]) {
remove(this.keys, key)
this.keys.push(key)
return this.cache[key]
}
return -1
}
LRUCache.prototype.put = function (key, value) {
if (this.cache[key]) {
this.cache[key] = value
remove(this.keys, key)
this.keys.push(key)
} else {
this.keys.push(key)
this.cache[key] = value
if (this.keys.length > this.capacity) {
removeCache(this.cache, this.keys, this.keys[0])
}
}
}
function remove(arr, key) {
if (arr.length) {
const index = arr.indexOf(key)
if (index > -1) {
return arr.splice(index, 1)
}
}
}
function removeCache(cache, keys, key) {
cache[key] = null
remove(keys, key)
}
const lRUCache = new LRUCache(2)
console.log(lRUCache.put(1, 1))
console.log(lRUCache.put(2, 2))
console.log(lRUCache.get(1))
console.log(lRUCache.put(3, 3))
console.log(lRUCache.get(2))
console.log(lRUCache.put(4, 4))
console.log(lRUCache.get(1) )
console.log(lRUCache.get(3))
console.log(lRUCache.get(4) )
var LRUCache = function (capacity) {
this.cache = new Map()
this.capacity = capacity
};
LRUCache.prototype.get = function (key) {
if (this.cache.has(key)) {
const value = this.cache.get(key)
this.cache.delete(key)
this.cache.set(key, value)
return value
}
return -1
};
LRUCache.prototype.put = function (key, value) {
if (this.cache.has(key)) {
this.cache.delete(key)
} else {
if (this.cache.size >= this.capacity) {
this.cache.delete(this.cache.keys().next().value)
}
}
this.cache.set(key, value)
};
const lRUCache1 = new LRUCache(2)
console.log(lRUCache.put(1, 1))
console.log(lRUCache.put(2, 2))
console.log(lRUCache.get(1))
console.log(lRUCache.put(3, 3))
console.log(lRUCache.get(2))
console.log(lRUCache.put(4, 4))
console.log(lRUCache.get(1) )
console.log(lRUCache.get(3))
console.log(lRUCache.get(4) )