Quickly create an array of classes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.