class Scheduler {
constructor () {
this.queue = []
this.maxCount = 2
this.runCount = 0
}
add(promiseCreator) {
this.queue.push(promiseCreator)
this.runQueue()
}
runQueue () {
if (this.queue.length && this.runCount < this.maxCount) {
const promiseCreator = this.queue.shift()
this.runCount += 1
promiseCreator().then(() => {
this.runCount -= 1
this.runQueue()
})
}
}
}
const timeout = time => {
return new Promise(resolve => {
setTimeout(resolve, time)
})
}
const scheduler = new Scheduler()
const addTask = (time,order) => {
scheduler.add(() => timeout(time).then(()=>console.log(order)))
}
addTask(1000, '1')
addTask(500, '2')
addTask(300, '3')
addTask(400, '4')
const simulateSetTimeout = (fn, timeout) => {
let timer = null
timer = setInterval(() => {
clearInterval(timer)
fn()
}, timeout)
return () => clearInterval(timer)
}
const cancel = simulateSetTimeout(() => {
console.log(1)
}, 1000)
setTimeout(() => {
cancel()
}, 1100)
const jsonstringify = jsonStringify = (data) => {
}
console.log(jsonstringify(undefined))
console.log(jsonstringify(() => { }))
console.log(jsonstringify(Symbol('前端胖头鱼')))
console.log(jsonstringify((NaN)))
console.log(jsonstringify((Infinity)))
console.log(jsonstringify((null)))
console.log(jsonstringify({
name: '前端胖头鱼',
toJSON() {
return {
name: '前端胖头鱼2',
sex: 'boy'
}
}
}))
console.log(jsonstringify(null) === JSON.stringify(null));
console.log(jsonstringify(undefined) === JSON.stringify(undefined));
console.log(jsonstringify(false) === JSON.stringify(false));
console.log(jsonstringify(NaN) === JSON.stringify(NaN));
console.log(jsonstringify(Infinity) === JSON.stringify(Infinity));
let str = "前端胖头鱼";
console.log(jsonstringify(str) === JSON.stringify(str));
let reg = new RegExp("\w");
console.log(jsonstringify(reg) === JSON.stringify(reg));
let date = new Date();
console.log(jsonstringify(date) === JSON.stringify(date));
let sym = Symbol('前端胖头鱼');
console.log(jsonstringify(sym) === JSON.stringify(sym));
let array = [1, 2, 3];
console.log(jsonstringify(array) === JSON.stringify(array));
let obj = {
name: '前端胖头鱼',
age: 18,
attr: ['coding', 123],
date: new Date(),
uni: Symbol(2),
sayHi: function () {
console.log("hello world")
},
info: {
age: 16,
intro: {
money: undefined,
job: null
}
},
pakingObj: {
boolean: new Boolean(false),
string: new String('前端胖头鱼'),
number: new Number(1),
}
}
console.log(jsonstringify(obj) === JSON.stringify(obj));
console.log((jsonstringify(obj)))
console.log(JSON.stringify(obj))
let enumerableObj = {}
Object.defineProperties(enumerableObj, {
name: {
value: '前端胖头鱼',
enumerable: true
},
sex: {
value: 'boy',
enumerable: false
},
})
console.log(jsonstringify(enumerableObj))
let obj1 = { a: 'aa' }
let obj2 = { name: '前端胖头鱼', a: obj1, b: obj1 }
obj2.obj = obj2
console.log(jsonStringify(BigInt(1)))
console.log(JSON.stringify({ name: '前端胖头鱼', sex: 'boy' }))
console.log(JSON.stringify('前端胖头鱼'))
console.log(JSON.stringify(1))
console.log(JSON.stringify(true))
console.log(JSON.stringify(null))
console.log(JSON.stringify({ name: '前端胖头鱼', sex: 'boy', age: 100 }, (key, value) => {
return typeof value === 'number' ? undefined : value
}))
console.log(JSON.stringify({ name: '前端胖头鱼', sex: 'boy', age: 100 }, [ 'name' ]))
console.log(JSON.stringify({ name: '前端胖头鱼', sex: 'boy', age: 100 }))
console.log(JSON.stringify({ name: '前端胖头鱼', sex: 'boy', age: 100 }, null , 2))
console.log(JSON.stringify({
name: '前端胖头鱼',
sex: 'boy',
showName () {
console.log('前端胖头鱼')
},
age: undefined,
symbolName: Symbol('前端胖头鱼')
}))
console.log(JSON.stringify([
'前端胖头鱼',
'boy',
function showName () {
console.log('前端胖头鱼')
},
undefined,
Symbol('前端胖头鱼')
]))
console.log(JSON.stringify(
function showName () {
console.log('前端胖头鱼')
}
))
console.log(JSON.stringify(undefined))
console.log(JSON.stringify(Symbol('前端胖头鱼')))
console.log(JSON.stringify([new Number(1), new String("前端胖头鱼"), new Boolean(false)]))
console.log(JSON.stringify({
name: Symbol('前端胖头鱼'),
}))
console.log(JSON.stringify({
[ Symbol('前端胖头鱼') ]: '前端胖头鱼',
}, (key, value) => {
if (typeof key === 'symbol') {
return value
}
}))
console.log(JSON.stringify({
age: NaN,
age2: Infinity,
name: null
}))
const toJSONObj = {
name: '前端胖头鱼',
toJSON () {
return 'JSON.stringify'
}
}
console.log(JSON.stringify(toJSONObj))
const d = new Date()
console.log(d.toJSON())
console.log(JSON.stringify(d))
let cyclicObj = {
name: '前端胖头鱼',
}
cyclicObj.obj = cyclicObj
let enumerableObj1 = {}
Object.defineProperties(enumerableObj, {
name: {
value: '前端胖头鱼',
enumerable: true
},
sex: {
value: 'boy',
enumerable: false
},
})
console.log(JSON.stringify(enumerableObj))
const alsoHuge = BigInt(9007199254740991)
console.log(JSON.stringify(alsoHuge))