In the actual development, we may use many JavaScript built-in methods, and these methods do bring us a lot of convenience and greatly improve our code path. However, for a moment, do you want to know how these methods are implemented? Next, I will use a few blogs to understand
What can you learn from this article?
- Re consolidate the use of various methods
- Consolidate the foundation. Even without these methods, it can be realized by basic grammar
Slice
Parameter represents meaning
- start: the index of the string to intercept (including this character)
- End: end the intercepted string index (excluding this character)
Attention
- Start > end: returns an empty string
- Start < 0: start = array length + start
String.prototype.slice = function (start = 0, end) { start = start < 0 ? this.length + start : start end = !end && end !== 0 ? this.length : end if (start >= end) return '' let str = '' for (let i = start; i < end; i++) { str += this[i] } return str } // const str = 'surprise_me' console.log(str.slice(2)) // rprise_me console.log(str.slice(-2)) // me console.log(str.slice(-9, 10)) // rprise_m console.log(str.slice(5, 1)) // ''
Substr
Parameter represents meaning
- start: the index of the string to intercept (including this character)
- Length: intercepted length
Attention
- Length < 0: returns an empty string
- Start < 0: start = array length + start
- The length exceeds the interception range and needs to be processed
String.prototype.substr = function (start = 0, length) { if (length < 0) return '' start = start < 0 ? this.length + start : start length = (!length && length !== 0) || length > this.length - start ? this.length : start + length let str = '' for (let i = start; i < length; i++) { str += this[i] } return str } // const str = 'surprise_me' console.log(str.substr(3)) // prise_me console.log(str.substr(3, 3)) // pri console.log(str.substr(5, 300)) // ise_me
Substring
Parameter represents meaning
- Similar to slice function
Differences
- Start > end: swap values
String.prototype.substring = function (start = 0, end) { start = start < 0 ? this.length + start : start end = !end && end !== 0 ? this.length : end if (start >= end) [start, end] = [end, start] let str = '' for (let i = start; i < end; i++) { str += this[i] } return str } // const str = 'surprise_me' console.log(str.substring(2)) // rprise_me console.log(str.substring(-2)) // surprise_me console.log(str.substring(-9, 10)) // surprise_m console.log(str.substring(5, 1)) // urprPromise
All
- Accept a Promise array. If there are non Promise items in the array, this item will be regarded as successful
- If all promises are successful, the success result array is returned
- If a Promise fails, this failure result is returned
function all(promises) { const result = [] let count = 0 return new MyPromise((resolve, reject) => { const addData = (index, value) => { result[index] = value count++ if (count === promises.length) resolve(result) } promises.forEach((promise, index) => { if (promise instanceof MyPromise) { promise.then(res => { addData(index, res) }, err => reject(err)) } else { addData(index, promise) } }) }) }
Race
- Receive a Promise array. If there is a non Promise item in the array, this item is regarded as successful
- Whichever Promise gets the fastest result, it will return which result, regardless of success or failure
function race(promises) { return new MyPromise((resolve, reject) => { promises.forEach(promise => { if (promise instanceof MyPromise) { promise.then(res => { resolve(res) }, err => { reject(err) }) } else { resolve(promise) } }) }) }
AllSettled
- Receive a Promise array. If there is a non Promise item in the array, this item is regarded as successful
- Combine the results of each Promise into an array, and then return
function allSettled(promises) { return new Promise((resolve, reject) => { const res = [] let count = 0 const addData = (status, value, i) => { res[i] = { status, value } count++ if (count === promises.length) { resolve(res) } } promises.forEach((promise, i) => { if (promise instanceof MyPromise) { promise.then(res => { addData('fulfilled', res, i) }, err => { addData('rejected', err, i) }) } else { addData('fulfilled', promise, i) } }) }) }
Any
any is the opposite of all
- Accept a Promise array. If there are non Promise items in the array, this item will be regarded as successful
- If a Promise succeeds, the success result is returned
- If all promises fail, an error is reported
function any(promises) { return new Promise((resolve, reject) => { let count = 0 promises.forEach((promise) => { promise.then(val => { resolve(val) }, err => { count++ if (count === promises.length) { reject(new AggregateError('All promises were rejected')) } }) }) }) } }
Conclusion
If you think this article is a little helpful to you, give a praise and encourage ah Lei, ha ha.