Extend native JavaScript array

后端 未结 10 889
情书的邮戳
情书的邮戳 2020-11-27 22:08

Is there any way to inherit a class from JS native function?

For example, I have a JS function like this:

function Xarray()
{
    Array.apply(this, a         


        
10条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 22:57

    Don't know how frowned upon this is but for example I needed to create an array of BulletTypes so that I could cycle through them. What I did is the following:

    interface BulletTypesArray extends Array {
        DefaultBullet?: Bullet; 
    }
    
    var BulletTypes: BulletTypesArray = [ GreenBullet, RedBullet ];
    BulletTypes.DefaultBullet = GreenBullet;
    

    Obviously you could could also make a generic interface, something like interface SuperArray extends Array.

提交回复
热议问题