使用[]运算符时,在Typescript 1.6.2中从内置数组扩展的类不会更新长度 [英] Class extended from built-in Array in Typescript 1.6.2 does not update length while using [] operator

查看:103
本文介绍了使用[]运算符时,在Typescript 1.6.2中从内置数组扩展的类不会更新长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我读到这里


TypeScript 1.6增加了对扩展任意表达式的类的支持,这些类计算构造函数。这意味着现在可以在类声明中扩展内置类型。

TypeScript 1.6 adds support for classes extending arbitrary expression that computes a constructor function. This means that built-in types can now be extended in class declarations.

...

一些例子:

// Extend built-in types

class MyArray extends Array<number> { }
class MyError extends Error { }

...


但是在扩展Array时,使用[]运算符设置值时不会更新length属性。推送功能工作正常,但我需要[]运算符。

However when extending Array, length property is not updated while using [] operator to set values. Push function works fine, but I need [] operator.

我的例子:

class ExtendedArray<T> extends Array<T> {}

var buildinArray = new Array<string>();
var extendedArray = new ExtendedArray<string>();

buildinArray.push("A");
console.log(buildinArray.length); // 1 - OK
buildinArray[2] = "B";
console.log(buildinArray.length); // 3 - OK

extendedArray.push("A");
console.log(extendedArray.length); // 1 - OK
extendedArray[2] = "B";
console.log(extendedArray.length); // 1 - FAIL
console.dir(extendedArray); // both values, but wrong length

我做错了什么?问题出在哪里?

Am I doing something wrong? Where is the problem?

推荐答案

您的代码转换为此JavaScript代码:

Your code translates to this JavaScript code:

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var ExtendedArray = (function (_super) {
    __extends(ExtendedArray, _super);
    function ExtendedArray() {
        _super.apply(this, arguments);
    }
    return ExtendedArray;
})(Array);
var buildinArray = new Array();
var extendedArray = new ExtendedArray();
buildinArray.push("A");
console.log(buildinArray.length); // 1 - OK
buildinArray[2] = "B";
console.log(buildinArray.length); // 3 - OK
extendedArray.push("A");
console.log(extendedArray.length); // 1 - OK
extendedArray[2] = "B";
console.log(extendedArray.length); // 1 - FAIL
console.dir(extendedArray); // both values, but wrong length

[ Playground ]

问题是括号表示法不是作为复制数组原型的一部分而复制的:

The problem is that the bracket notation is not copied as part of copying of Array prototype:


在JavaScript中,我们可以通过扩展
本机原型来对本机数据类型进行子类化。这与本机String对象完美配合;
但是,当谈到本机阵列时,事情并不能很好地工作。如果我们扩展Array原型,我们继承本机数组
函数;但是,我们不再能够使用括号表示法
来设置和获取给定数组中的索引值。当然,我们可以使用
push()和pop()来克服这个限制;但是,如果我们想保持
括号表示功能的功能,我们必须在
现有数组实例的基础上构建,而不是真正对Array
对象进行子类化。

In JavaScript, we can sub-class native data types by extending the native prototypes. This works perfectly with the native String object; but, when it comes to native Arrays, things don't work quite so nicely. If we extend the Array prototype, we inherit the native array functions; but, we no longer have the ability to use bracket notation to set and get indexed values within the given array. Sure, we can use push() and pop() to overcome this limitation; but, if we want to keep the bracket notation feature functional, we have to build on top of an existing array instance rather than truly sub-classing the Array object.

[来源]

有一个深入的讨论和解决方案

这篇关于使用[]运算符时,在Typescript 1.6.2中从内置数组扩展的类不会更新长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆