Parasitic constructor-extends native Arrays

Source: Internet
Author: User

Recently, during product R & D, we found that native arrays need to be expanded. We are well aware of the disadvantages of modifying native objects, especially modifying native object prototypes in productized programs. If a method is missing in an implementation, add this method to the prototype of the native object. When the code is run in another implementation that supports this method, this may cause a name conflict. In addition, this may be intended to rewrite native methods in other places. This is also the reason why the well-known prototype framework has changed a large number of native objects. When ECMAscript is upgraded, there are a lot of problems with renaming rewrite methods and native methods, so we need to constantly change these original extension names, resulting in a series of incidental problems. In my work, I used the "Parasitic constructor mode" to expand native arrays. Although this method is not widely used, it can be used to solve such problems, this method is the most effective. If the traditional composite constructor and prototype are used, the native array is modified because the native object is extended. What is the parasitic constructor pattern like? [Javascript] var myArray = function () {// create a new object var arr = []; // obtain the value of arr. indexOf = function (item) {for (var I = 0, len = arr. length; I <len; I ++) {if (arr [I] === item) {return I ;}} return-1 ;}; // check whether a value is in the array. hasItem = function (item) {return arr. indexOf (item )! =-1 ;}; // the existing values in the array cannot be inserted. An array arr can also be input here. append = function (aItem) {function _ push (aItem) {var aExt = []; for (var I = 0, l = aItem. length; I <l; I ++) {if (! Arr. hasItem (aItem [I]) {arr. push (aItem [I]);} else {aExt. push (aItem [I]) ;}} if (aExt. length> 0) {alert (aExt. toString () + 'has in Array! '); AExt = null ;}}; if ($. isArray (aItem) {_ push (aItem) ;}else {_ push (arguments) ;}return arr ;}; // remove a value or an array arr. remove = function (aItem) {function _ splice (a) {for (var I = 0, l =. length; I <l; I ++) {var item = a [I]; if (arr. hasItem (item) {return arr. splice (arr. indexOf (item), 1) ;}}; if ($. isArray (aItem) {_ splice (aItem) ;}else {_ splice (arguments) ;}return arr ;}; // clear the array arr. ClearAll = function () {arr. length = 0; return arr ;}; // insert a value in an index. insert = function (target, indexPos) {var len = arguments. length; if (len = 2) {var arg = arguments [0]; if (! Arr. hasItem (arg) {arr. splice (arguments [1], 0, arg);} else {alert (arg + 'has in Array! ') ;}} Else {alert ('insert args count error! ');} Return arr ;}; // remove repeated arr. removeRepeat = function () {var json = {}, res = [], l = arr. length; if (l> 0) {json [arr [0] = true; res. push (arr [0]); for (var I = 1; I <l; I ++) {if (! Json [arr [I]) {json [arr [I] = true; res. push (arr [I]) ;}} return res ;}; // return a new object. this return arr;} is returned when the new object is rewritten to call the constructor ;}; in this example, the myArray function creates a new object, initializes the object with the corresponding attributes and methods, and then returns the object. In addition to using the new operator and calling the wrapped function constructor, this mode is exactly the same as the factory mode. If no value is returned, the constructor returns the new object instance by default. By adding a return statement at the end of the constructor, You can override the value returned when calling the constructor. Note the parasitic constructor mode: first, the returned object has no relationship with the constructor or the prototype attribute of the constructor. That is to say, the objects returned by the constructor are no different from those created outside the constructor. Therefore, the instanceof operator cannot be used to determine the object type. Because of the above problems, the object instance type created in this mode can only be object.

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.