深拷贝
function deepClone ( ori ) {
let tar;
if ( typeof ori === 'object' && ori !== null ) {
tar = Array. isArray ( ori) ? [ ] : {
}
for ( let k in ori) {
if ( ori. hasOwnProperty ( k) ) {
tar[ k] = deepClone ( ori[ k] )
}
}
} else {
tar = ori
}
return tar
}
继承
function Parent ( ) {
}
function Child ( ) {
}
function Buffer ( ) {
}
Buffer . prototype = Parent . prototype
Child . prototype = new Buffer ( )
Child . prototype. constructor = Child
Child . prototype. super_class = Parent
class Parent {
}
class Child extends Parent {
constructor ( ) {
super ( )
}
}
function Child ( ) {
Parent . call ( this )
}
Child . prototype = Object. create ( Parent . prototype)
Child . prototype. constructor = Child
Promise
const pending = Symbol ( 'pending' )
const fulfilled = Symbol ( 'fulfilled' )
const rejected = Symbol ( 'rejected' )
class Promise {
constructor ( executor ) {
this . status = 'pending'
this . value = undefined
this . reason = undefined
this . onFulfilledCbs = [ ]
this . onRejectedCbs = [ ]
function resolve ( value ) {
if ( this . status === 'pending' ) {
this . status = 'fulfilled'
this . value = value
this . onFulfilledCbs. forEach ( fn => fn ( ) )
}
}
function reject ( reason ) {
if ( this . status === 'pending' ) {
this . status = 'rejected'
this . reason = reason
this . onRejectedCbs. forEach ( fn => fn ( ) )
}
}
try {
executor ( resolve, reject)
} catch ( err) {
reject ( err)
}
}
then ( onFulfilled, onRejected ) {
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : value => value
onRejected = typeof onRejected === 'function' ? onRejected : reason => reason
let promise = new Promise ( ( resolve, reject ) => {
if ( this . status === 'fulfilled' ) {
setTimeout ( ( ) => {
try {
let x = onFulfilled ( this . value)
resolvePromise ( promise, x, resolve, reject)
} catch ( e) {
reject ( e)
}
} ) ;
}
if ( this . status === 'rejected' ) {
setTimeout ( ( ) => {
try {
let x = onRejected ( this . reason)
resolvePromise ( promise, x, resolve, reject)
} catch ( e) {
reject ( e)
}
} ) ;
}
if ( this . status === 'pending' ) {
this . onFulfilledCbs. push ( ( ) => {
setTimeout ( ( ) => {
try {
let x = onFulfilled ( this . value)
resolvePromise ( promise, x, resolve, reject)
} catch ( e) {
reject ( e)
}
} ) ;
} )
this . onRejectedCbs. push ( ( ) => {
setTimeout ( ( ) =>