Failed attempts, using an inherited extended array, and the necessity of ES6

Source: Internet
Author: User
Tags es6 class

We all know it's not good to extend an object directly on a native object. So prototype such a library is widely criticized.

Some libraries, such as Lodash, use a toolkit-style extension to bypass the inheritance of objects.

Because of the appearance of the Es6 class, I tried to extend it in the way that the array derives the child object.

Here is a simple example.

' Use strict 'class ABC extends array{  GetArray () {    returnthis;  } Let x=new ABC (5). Fill (1); Console.log (X.getarray ());

In V8, this example will output the correct result [1,1,1,1,1].

Thereafter, in order to be compatible with other browsers, I tried to use babel.js transcoding to ES5.

But no matter how this step was unsuccessful.

Finally I went into Babel's official discussion group only to know that this was not supported.

Loganfsmyth gives a notation

class Extendablearray {    constructor (... args) {        new  Array (... args);          This . Constructor.prototype);         return arr;    }}

But obviously, instanceof and Array.isarray and so on will have problems.

Therefore, there is no perfect alternative at this stage.

Many people say that many of the things in es6 are grammatical sugars.

Yes, that's true.

However, as the scripting language, the more refined the script, the faster the scanning speed, the performance potential of the compressor will increase.

Lengthy grammar has never been meaningful, needed, or written in a neat way.

Many of the features of ES6 are useful for these points.

The appearance of the module unifies the current cumbersome loading mode.

A series of asynchronous statements have been proposed to solve the callback hell.

The proposed class simplifies the inheritance process.

The more basic data structures optimize the data processing performance.

Although efficiency is not taken into account, ES6 can do it, and ES5 can do most of it.

But ES5 can't do better.

program, must be graceful.

Failed attempts, using an inherited extended array, and the necessity of ES6

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.