Implementation of the native built-in functions of the # JS array ## forEachArray.prototype.myforEach = function (callback, thisarg) {
if (typeof this= = ='undefined' || this= = =null) {
throw new TypeError('this is null or not defined')}else if (typeofcallback ! = ='function') {
throw new TypeError(callback + ' is not a function')}else {
var i = 0
var len = this.length >>> 0
var arr = Object(this)
while (len > i) {
if (i in arr) {
callback.call(thisarg, arr[i], i, arr)
}
i++
}
}
}
## map
Array.prototype.mymap = function (callback, thisarg) {
if (typeof this= = ='undefined' || this= = =null) {
throw new TypeError('this is null or not defined')}else if (typeofcallback ! = ='function') {
throw new TypeError(callback + ' is not a function')}else {
var i = 0
var len = this.length >>> 0
var arr = Object(this)
var res = []
while (len > i) {
if (i in arr) {
res[i] = callback.call(thisarg, arr[i], i, arr)
}
i++
}
return res
}
}
## filter
Array.prototype.myfilter = function (callback, thisarg) {
if (typeof this= = ='undefined' || this= = =null) {
throw new TypeError('this is null or not defined')}else if (typeofcallback ! = ='function') {
throw new TypeError(callback + ' is not a function')}else {
var i = 0
var len = this.length >>> 0
var arr = Object(this)
var res = []
while (len > i) {
if (i in arr) {
callback.call(thisarg, arr[i], i, arr) && res.push(arr[i])
}
i++
}
return res
}
}
## some
Array.prototype.mysome = function (callback, thisarg) {
if (typeof this= = ='undefined' || this= = =null) {
throw new TypeError('this is null or not defined')}else if (typeofcallback ! = ='function') {
throw new TypeError(callback + ' is not a function')}else {
var i = 0
var len = this.length >>> 0
var arr = Object(this)
while (len > i) {
if (i in arr) {
if (callback.call(thisarg, arr[i], i, arr))
return true
}
i++
}
return false
}
}
## reduce
Array.prototype.myreduce = function (callback, defaultValue) {
if (typeof this= = ='undefined' || this= = =null) {
throw new TypeError('this is null or not defined')}else if (typeofcallback ! = ='function') {
throw new TypeError(callback + ' is not a function')}else {
var i = 0
var len = this.length >>> 0
var arr = Object(this)
var count = defaultValue || arr[i++] // If there is no initial value, the first non-empty value in the array is taken and the loop begins with the next item
while (len > i) {
if (i in arr) {
count = callback(count, arr[i], i, arr)
}
i++
}
returnCount}} ##Array.prototype.removeRepeat = function () {
if (this instanceof Array) {
var arr = this
var len = arr.length
for (var i = 0; i < len; i++) {
for (var j = i + 1; j < len; j++) {
if (arr[i] === arr[j]) {
arr.splice(j, 1)
len--
j--
}
}
}
return arr
} else {
throw new TypeError(this + ' is not a Array'}} # changethisCall,apply,bind ## callFunction.prototype.mycall = function (context, ... arg) {
context = context || window
context.fn = this
varres = context.fn(... arg)delete context.fn
return res
}
Function.prototype.mycall2 = function (context) {
context = context || window
context.fn = this
var len = arguments.length
var i = 1
var arg = []
while (i < len) {
arg.push(arguments[i++])
}
var res = eval('context.fn(' + arg + ') ')
delete context.fn
return res
}
## apply
Function.prototype.myapply = function (context, arg) {
var context = context || window
context.fn = this
var res = eval('context.fn(' + arg + ') ')
delete context.fn
return res
}
## bind
Function.prototype.mybind = function () {
var context = window
var len = arguments.length
if (len > 0) {
context = arguments[0]
}
context.fn = this
var i = 1
var args = [] // [].slice.call(arguments, 1)
while (i < len) {
args.push(arguments[i++])
}
return function () {
var res = eval('context.fn(' + args + ') ')
delete context.fn
return res
}
}
# ObjectBuilt-in function ##Object.create
Object.myCreate = function (proto, propertyObject = undefined) {
if (typeofproto ! = ='object' && typeofproto ! = ='function') {
throw new TypeError('Object prototype may only be an Object or null.')}if (propertyObject == null) {
new TypeError('Cannot convert undefined or null to object')}function fn () {}
fn.prototype = proto
const f = new fn()
if(propertyObject ! =undefined) {
Object.defineProperties(f, propertyObject)
}
if (proto === null) {
f.__proto__ = null
}
return f
}
## Object.assign
Object.myAssign = function (target, ... source) {
if (target == null) {
throw new TypeError('cannot convert undefined or null to object')}let obj = Object(target)
source.forEach(item= > {
if(item ! =null) {
for (let key in item) {
if (item.hasOwnProperty(key)) {
obj[key] = item[key]
}
}
}
})
returnObj} # other ##new
function mynew() {
var obj = new Object(a)var constructor = [].shift.call(arguments)
obj.__proto__ = constructor.prototype
var ret = constructor.apply(obj, arguments)
return typeof ret= = = 'object'?ret || obj : obj} # #instanceof
function myInstanceof(leftobj, rightobj) {
return leftobj.__proto__ === rightobj.prototype
}
## JSON.parse
JSON.myParse = function (jsonstr) {
return (new Function('return '+ jsonstr))(); } -- Any problems are welcome to point outCopy the code