Home > front end >  To simulate the every method, there are two questions want to consult
To simulate the every method, there are two questions want to consult

Time:12-06

 Array. Prototype. _every=function (func, arr) {
Var newArr=arr==undefined? Arr=this: arr;
If (arr. Length==0) return true;
If (typeof fn!=='function') {
Throw new TypeError (fn + 'is not a function');
}
For (the let I=0; i if (! Func (newArr [I]) {//if one of them returns false,! Instead get back to the true, the result returns false
return false;
}
}
return true;
};

The function check (item) {
Return the item & gt;=10;
}

Let arr=[16, 32, 33, 40];

Let the result=arr. _every (" check ");
console.log(result);//true


1) the browser is not throw an error function, and this have what use?
If (typeof fn!=='function') {
Throw new TypeError (fn + 'is not a function');
}
2) why don't I can new Array (), you can call _every method?










CodePudding user response:

1. Take the initiative to throw an exception 2. _every is to mount an Array object prototype chain method as Array as an Array of other methods of new don't like literal instance of new can't push an Array slice

CodePudding user response:

1. Every need to callback function called, determine whether to function, if not, will take the initiative to throw an exception
2. To the prototype of the Array method because of you, this is a reference, all instances of the object, has a __proto__ attribute to a prototype
3. Do you have in this method, only allow the callback function using the item, to get each item, but without a index and arr, normal array of these methods, such as the forEach, some, reduce can be in a callback function, get the three parameters

CodePudding user response:

I understand
 function Person () {
}
Person. The prototype. _every=function () {
The return of 111;
};

Alert (Person. _every ());


Person. _every () if not new Person is printed out in advance, and wanted to know why the Array can not new can run
  • Related