# fill 原理
# 使用 for 实现
| Array.prototype._fill = function (value = [], start = 0, end = this.length) { |
| |
| if (start > this.length || end > this.length) return this |
| |
| |
| start = start < 0 ? this.length + start : start |
| end = end < 0 ? this.length + end : end |
| |
| for (let i = start; i < end; i++) { |
| |
| if (!arguments.length) this[i] = undefined |
| else this[i] = value |
| } |
| return this |
| } |
# 测试
| |
| [1, 2, 3].fill() |
| [1, 2, 3].fill(4) |
| [1, 2, 3].fill(4, 1) |
| [1, 2, 3].fill(4, 1, 2) |
| [1, 2, 3].fill(4, 1, 1) |
| [1, 2, 3].fill(4, 3, 3) |
| [1, 2, 3].fill(4, -3, -2) |
| [1, 2, 3].fill(4, NaN, NaN) |
| [1, 2, 3].fill(4, 3, 5) |
| Array(3).fill(4) |
| [].fill.call({ length: 3 }, 4) |
| |
| |
| [1, 2, 3]._fill() |
| [1, 2, 3]._fill(4) |
| [1, 2, 3]._fill(4, 1) |
| [1, 2, 3]._fill(4, 1, 2) |
| [1, 2, 3]._fill(4, 1, 1) |
| [1, 2, 3]._fill(4, 3, 3) |
| [1, 2, 3]._fill(4, -3, -2) |
| [1, 2, 3]._fill(4, NaN, NaN) |
| [1, 2, 3].fill(4, 3, 5) |
| Array(3)._fill(4) |
| []._fill.call({ length: 3 }, 4) |
# 使用 for in 实现
| Array.prototype._fill = function (value = [], start = 0, end = this.length) { |
| |
| if (start > this.length || end > this.length) return this |
| |
| |
| start = start < 0 ? this.length + start : start |
| end = end < 0 ? this.length + end : end |
| |
| |
| const commaLength = this.length - 1 |
| let comma = '' |
| for (let i = 0; i < commaLength; i++) comma += ',' |
| |
| |
| if (this + '' == comma || this + '' === '[object Object]') { |
| for (let i = 0; i < end; i++) { |
| this[i] = value |
| } |
| return this |
| } |
| |
| |
| for (const key in this) { |
| if ('_fill' === key) return this |
| |
| if (key < start) continue |
| |
| |
| if (key < end) this[key] = value |
| |
| |
| if(!arguments.length) this[key] = undefined |
| } |
| } |
# 测试
| |
| [1, 2, 3].fill(4); |
| [1, 2, 3].fill(4, 1); |
| [1, 2, 3].fill(4, 1, 2); |
| [1, 2, 3].fill(4, 1, 1); |
| [1, 2, 3].fill(4, 3, 3); |
| [1, 2, 3].fill(4, -3, -2); |
| [1, 2, 3].fill(4, NaN, NaN); |
| [1, 2, 3].fill(4, 3, 5); |
| Array(3).fill(4); |
| [].fill.call({ length: 3 }, 4); |
| |
| |
| [1, 2, 3]._fill(4) |
| [1, 2, 3]._fill(4, 1) |
| [1, 2, 3]._fill(4, 1, 2) |
| [1, 2, 3]._fill(4, 1, 1) |
| [1, 2, 3]._fill(4, 3, 3) |
| [1, 2, 3]._fill(4, -3, -2) |
| [1, 2, 3]._fill(4, NaN, NaN) |
| [1, 2, 3].fill(4, 3, 5) |
| Array(3)._fill(4) |
| []._fill.call({ length: 3 }, 4) |
# join 原理
# 使用 for 实现
| Array.prototype._join = function (separator = ',') { |
| let str = '' |
| for (let i = 0; i < this.length; i++) |
| str = i === 0 ? `${this[i]}` : `${str}${separator}${this[i]}` |
| return str |
| } |
# 测试
| |
| const str = ['Wind', 'Rain', 'Fire'] |
| str.join() |
| str.join(', ') |
| str.join(' + ') |
| str.join('') |
| |
| |
| const str = ['Wind', 'Rain', 'Fire'] |
| str._join() |
| str._join(', ') |
| str._join(' + ') |
| str._join('') |
# 使用 for in 实现
| Array.prototype._join = function (value = ',') { |
| let str = '' |
| for (const key in this) { |
| if('_join' === key) return str |
| str = key == 0 ? `${str}${this[key]}` : `${str}${value}${this[key]}` |
| } |
| } |
# 测试
| |
| const str = ['Wind', 'Rain', 'Fire'] |
| str.join() |
| str.join(', ') |
| str.join(' + ') |
| str.join('') |
| |
| |
| const str = ['Wind', 'Rain', 'Fire'] |
| str._join() |
| str._join(', ') |
| str._join(' + ') |
| str._join('') |
# String includes 原理
# 使用 for 实现
| String.prototype._includes = function (searchString, position = 0) { |
| |
| if (searchString == this) return true |
| |
| |
| if (position > this.length) return false |
| |
| |
| const searchStringLength = searchString.toString().length |
| |
| |
| position = position < 0 ? 0 : position |
| |
| |
| let resultStr = '' |
| |
| |
| let cycleIndex = this.length - searchStringLength + 1 |
| |
| |
| |
| const getStr = (index) => { |
| for (let i = 0; i < searchStringLength; i++) |
| resultStr += this[index++] |
| return resultStr |
| } |
| |
| for (let i = position; i < cycleIndex; i++){ |
| |
| if (searchString != getStr(i)) resultStr = '' |
| else return true |
| } |
| |
| return false |
| } |
# 测试
| const str = 'To be, or not to be, that is the question.' |
| |
| str.includes('To be') |
| str.includes('question') |
| str.includes('nonexistent') |
| str.includes('To be', 1) |
| str.includes('TO BE') |
| |
| |
| str._includes('To be') |
| str._includes('question') |
| str._includes('nonexistent') |
| str._includes('To be', 1) |
| str._includes('TO BE') |
# 使用 while 实现
| String.prototype._includes = function (value, startIndex = 0) { |
| if (this == value) return true |
| |
| |
| const length = this.length |
| |
| |
| const valueLength = value.length |
| |
| |
| startIndex = startIndex < 0 ? 0 : startIndex |
| |
| let i = startIndex |
| |
| |
| while (i + valueLength <= length) { |
| let num = 0, str = '' |
| |
| |
| while (num < valueLength) str += this[i + num++] |
| i++ |
| |
| |
| if (str === value) return true |
| } |
| return false |
| } |
# 测试
| const str = 'To be, or not to be, that is the question.' |
| |
| str.includes('To be') |
| str.includes('question') |
| str.includes('nonexistent') |
| str.includes('To be', 1) |
| str.includes('TO BE') |
| |
| |
| str._includes('To be') |
| str._includes('question') |
| str._includes('nonexistent') |
| str._includes('To be', 1) |
| str._includes('TO BE') |
# Array includex 原理
# 使用 for 实现
| Array.prototype._includes = function (value, start = 0) { |
| if (start > this.length) return false |
| start = start < 0 ? this.length + start : start |
| |
| for (let i = start; i < this.length; i++){ |
| if(this[i] === value || Number.isNaN(this[i])) return true |
| } |
| return false |
| } |
# 测试
| |
| [1, 2, 3].includes(3, -1) |
| [1, 2, 3].includes(2) |
| [1, 2, 3].includes(4) |
| [1, 2, 3].includes(3, 3) |
| [1, 2, NaN].includes(NaN) |
| |
| |
| [1, 2, 3].includes(2) |
| [1, 2, 3].includes(4) |
| [1, 2, 3].includes(3, 3) |
| [1, 2, 3].includes(3, -1) |
| [1, 2, NaN].includes(NaN) |
# 使用 for in 实现
| Array.prototype._includes = function (value, start = 0) { |
| if (start > this.length) return false |
| start = start < 0 ? this.length + start : start |
| |
| for (const key in this) { |
| if(key < start) continue |
| if(value === this[key] || Number.isNaN(this[key]) === Number.isNaN(NaN)) return true |
| } |
| return false |
| } |
# 测试
| |
| [1, 2, 3].includes(3, -1) |
| [1, 2, 3].includes(2) |
| [1, 2, 3].includes(4) |
| [1, 2, 3].includes(3, 3) |
| [1, 2, NaN].includes(NaN) |
| |
| |
| [1, 2, 3].includes(2) |
| [1, 2, 3].includes(4) |
| [1, 2, 3].includes(3, 3) |
| [1, 2, 3].includes(3, -1) |
| [1, 2, NaN].includes(NaN) |