To create a new object: function Createlikearray () {}; var New // {}
This is just an empty object, how to convert an object into an array of classes??
[1, 2, 3, 4] = = [0=>1, 1=>2, 2=>3, 3=>4] + = {' 0 ': 1, ' 1 ': 2, ' 2 ': ' 3 ', ' 3 ': ' 4 '};
Three steps are listed above:
function Createlikearray (arr) { thisfunction(array) { var This ; Array.foreach (function(V, i) { = v; });}
This output is still an object, unlike an array;
So what happens next?
function Createlikearray (arr) { this. Addkeys (arr);} var protos = Createlikearray.prototype ==function(array) { varthis; Array.foreach (function(v) { _this.push (v); });
ok! to my requirements, the data structure looks like an array, and has its own prototype, as well as an array of property methods;
Createlikearray: constructor function;
The Addkeys method actually turns the array into an object, which is not like an array;
Plus the prototype is an empty array, and it inherits all the methods of the array's prototype; this looks like an array;
In the following can continue to add properties and methods to Protos, expand the Createlikearray prototype;
When studying the jquery source code, see how jquery is implemented:
Finds the DOM, returns the array form, and then iterates over the array into {0: ', 1: ', Length:2,...};
Finally add the slice, splice method to the prototype, looking like an array
Quickly create an array of classes