Have seen a sentence before the foundation is not necessarily simple, has not realized, today finally saw. Reflect on their work before the study, a summary of words, very impetuous. Not much to say, a small example of the challenge.
Array.from (). Yes, you're right. This simple API is used to convert the data structure with the length property to an array, and you can use map to process each generated value before conversion. See here you may be a little bit, very simple? Isn't that a conversion?
Then let me take a few questions.
What is the compatibility of this API for Question 1:array.from ()? You will see IE not compatible. What if you want to use it? Let's see Polyfill realize it. And what is the process of polyfill implementation? What is the idea? What details do you need to pay attention to inside? I concluded that there was
1: Judging the data type, we look at Array.from (Param,fnc,thisarg). We write it ourselves, do we have to decide first whether FNC is a function? What's the use of that? To Object.prototype.call (FNC) = = = "[Object,function]"; Then there's a problem. What if I want to judge all the data types?
2: Use call. We passed in the FNC. How to send her into the current loop of the Item,index,arr, and change the FNC in this is Thisarg, this time use call. Call doesn't know? Maybe you go to call and find Apply,bind. is not to be compared. And then look at compatibility? Does it extend to other issues?
Problem 2:array.from can solve the problem of our actual programming? The base of the various type data with the length property into an array. What do these types include? Set,map,arguments,nodelist, string, or even {length:5}. Complex implementations of arrays are combined to remove weight. How to go to the heavy?
function Combine () { = [].concat.apply ([], arguments); // return array.from (newvar m = [1, 2, 2], n = [2,3,3
This is a simple implementation of ES6 in the middle of the weight. Then we have to consider his performance problems, time complexity, space complexity. What are some of the ways to implement a heavy weight? Don't you have to summarize. Weigh the time and space required for execution and choose the appropriate method of de-weighting.
For example, with a new array, or a new object, each element is added to the corresponding new array, in turn, in the new object. Then Judge indexof (item)! =-1 to join.!obj[item]. You can also use filter. ForEach is also implemented by IndexOf.
There is also a kind of realization by the source. The basic idea is to judge the current one and the next, if repeated, then ignore this element, remove an element, if not duplicated then add to the new array we created. Show Code
functionQC (arr) {if(Object.prototype.toString (arr)! = ' [object, Array] ') { ThrowNewError (' The QC need Array ')}varNEWARR = [] for(Let i=0,len=arr.length;i<len;i++){ for(Let j=i+1;j<len;j++){ //if repeated, I move forward, but no matter what the duplicates if(Arr[i]==arr[j]) j= + +i}//pushes no duplicates into the new arrayNewarr.push (Arr[i])}returnNEWARR}
To challenge your JS basic skills, the original thought that the foundation is solid in the castle. The knowledge point extended by Array.from. Includes array sorting, Call,apply,bind. Wait a minute.