当前位置:  开发笔记 > 编程语言 > 正文

ECMAScript规范是否允许Array为"超类"?

如何解决《ECMAScript规范是否允许Array为"超类"?》经验,为你挑选了0个好方法。

我正在寻找任何指示是否"超类"内置类型将根据规范工作.也就是说,假设任何假设符合ECMAScript的实现,"内置"的"超类"是否通过影响类构造函数的创建算法来破坏运行时?

"Superclassable",我正在创造的一个术语,指的是一个类,通过构造它来返回它,或者将它作为一个函数调用它,如果适用,将使用相同的内部插槽创建([[Prototype]]除外),无论如何它的直接超类是什么,只要类构造函数的初始[[Prototype]]和类原型在重新分配它们之后仍然在每个相应的继承链中.因此,为了成为"超类",类不得 super()在创建期间调用.

当"超级分类"时Array,我希望它看起来像这样:

// clearly this would break Array if the specification allowed an implementation
// to invoke super() internally in the Array constructor
class Enumerable {
  constructor (iterator = function * () {}) {
    this[Symbol.iterator] = iterator
  }

  asEnumerable() {
    return new Enumerable(this[Symbol.iterator].bind(this))
  }
}

function setSuperclassOf (Class, Superclass) {
  /* These conditions must be satisfied in order to
   * superclass Class with Superclass
   */
  if (
    !(Superclass.prototype instanceof Object.getPrototypeOf(Class.prototype).constructor) ||
    !(Superclass instanceof Object.getPrototypeOf(Class).constructor) ||
     (Superclass.prototype instanceof Class)
  ) {
    throw new TypeError(`${Class.name} cannot have their superclass set to ${Superclass.name}`)
  }
  
  // Now we can superclass Class with Superclass
  Object.setPrototypeOf(Class.prototype, Superclass.prototype)
  Object.setPrototypeOf(Class, Superclass)
}

setSuperclassOf(Array, Enumerable)

const array = new Array(...'abc')

// Checking that Array is not broken by Enumerable
console.log(array[Symbol.iterator] === Array.prototype[Symbol.iterator])

// Checking that Enumerable works as expected
const enumerable = array.asEnumerable()

console.log(array instanceof Enumerable)
console.log(!(enumerable instanceof Array))

for (const letter of enumerable) {
  console.log(letter)
}

我的一个最大的问题是,在内部,在可能的一致实现,Array 可能是这样,这将意味着Array不是 "superclassable":

class HypotheticalArray extends Object {
  constructor (...values) {
    const [value] = values

    // this reference would be modified by superclassing HypotheticalArray
    super()

    if (values.length === 1) {
      if (typeof value === 'number') {
        if (value !== Math.floor(value) || value < 0) {
          throw new RangeError('Invalid array length')
        }

        this.length = value
        return
      }
    }
    
    this.length = values.length

    for (let i = 0; i < values.length; i++) {
      this[i] = values[i]
    }
  }
  
  * [Symbol.iterator] () {
    const { length } = this

    for (let i = 0; i < length; i++) {
      yield this[i]
    }
  }
}

// Array constructor actually inherits from Function prototype, not Object constructor
Object.setPrototypeOf(HypotheticalArray, Object.getPrototypeOf(Function))

class Enumerable {
  constructor (iterator = function * () {}) {
    this[Symbol.iterator] = iterator
  }

  asEnumerable() {
    return new Enumerable(this[Symbol.iterator].bind(this))
  }
}

function setSuperclassOf (Class, Superclass) {
  /* These conditions must be satisfied in order to
   * superclass Class with Superclass
   */
  if (
    !(Superclass.prototype instanceof Object.getPrototypeOf(Class.prototype).constructor) ||
    !(Superclass instanceof Object.getPrototypeOf(Class).constructor) ||
     (Superclass.prototype instanceof Class)
  ) {
    throw new TypeError(`${Class.name} cannot have their superclass set to ${Superclass.name}`)
  }
  
  // Now we can superclass Class with Superclass
  Object.setPrototypeOf(Class.prototype, Superclass.prototype)
  Object.setPrototypeOf(Class, Superclass)
}

setSuperclassOf(HypotheticalArray, Enumerable)

const array = new HypotheticalArray(...'abc')

// Array is broken by Enumerable
console.log(array[Symbol.iterator] === HypotheticalArray.prototype[Symbol.iterator])

// Checking if Enumerable works as expected
const enumerable = array.asEnumerable()

console.log(array instanceof Enumerable)
console.log(!(enumerable instanceof HypotheticalArray))

// Iteration does not work as expected
for (const letter of enumerable) {
  console.log(letter)
}

但是,如果要求一致的实现不要调用,那么它Array "超类"的:super()

class HypotheticalArray {
  constructor (...values) {
    const [value] = values

    // doesn't ever invoke the superclass constructor
    // super()

    if (values.length === 1) {
      if (typeof value === 'number') {
        if (value !== Math.floor(value) || value < 0) {
          throw new RangeError('Invalid array length')
        }

        this.length = value
        return
      }
    }
    
    this.length = values.length

    for (let i = 0; i < values.length; i++) {
      this[i] = values[i]
    }
  }
  
  * [Symbol.iterator] () {
    const { length } = this

    for (let i = 0; i < length; i++) {
      yield this[i]
    }
  }
}

class Enumerable {
  constructor (iterator = function * () {}) {
    this[Symbol.iterator] = iterator
  }

  asEnumerable() {
    return new Enumerable(this[Symbol.iterator].bind(this))
  }
}

function setSuperclassOf (Class, Superclass) {
  /* These conditions must be satisfied in order to
   * superclass Class with Superclass
   */
  if (
    !(Superclass.prototype instanceof Object.getPrototypeOf(Class.prototype).constructor) ||
    !(Superclass instanceof Object.getPrototypeOf(Class).constructor) ||
     (Superclass.prototype instanceof Class)
  ) {
    throw new TypeError(`${Class.name} cannot have their superclass set to ${Superclass.name}`)
  }
  
  // Now we can superclass Class with Superclass
  Object.setPrototypeOf(Class.prototype, Superclass.prototype)
  Object.setPrototypeOf(Class, Superclass)
}

setSuperclassOf(HypotheticalArray, Enumerable)

const array = new HypotheticalArray(...'abc')

// Array is not broken by Enumerable
console.log(array[Symbol.iterator] === HypotheticalArray.prototype[Symbol.iterator])

// Checking if Enumerable works as expected
const enumerable = array.asEnumerable()

console.log(array instanceof Enumerable)
console.log(!(enumerable instanceof HypotheticalArray))

// Iteration works as expected
for (const letter of enumerable) {
  console.log(letter)
}

考虑到这一点,我想参考当前草案ECMAScript 2018中的几点:

§22.1.1数组构造函数

Array构造函数:

在作为构造函数调用时,创建并初始化一个新的Array异物对象.

被设计为可子类化.它可以用作类定义的extends子句的值.打算继承奇异数组行为的子类构造函数必须包含对Array构造函数的超级调用,以初始化作为Array外来对象的子类实例.

§22.1.3数组原型对象的属性

Array原型对象有一个[[Prototype]]内部插槽,其值是内部对象%ObjectPrototype%.

Array原型对象被指定为Array外来对象,以确保与ECMAScript 2015规范之前创建的ECMAScript代码兼容.

(重点补充)

我的理解是,一个一致实现以内部调用所需super()的范围内Array,以正确初始化实例作为阵列异国构造,也不需要Object是直接超类Array(虽然我的§22.1.3第一次报价似乎肯定暗示那一点).

我的问题是,上面的第一个片段是否按照规范工作,或者它是否只能工作,因为当前现有的实现允许它?即是第一个HypotheticalArray不符合的实施?

而对于全赏金奖,我也想申请这个问题String,Set,Map,和TypedArray(我的意思Object.getPrototypeOf(Uint8Array.prototype).constructor).

我将为第一个答案奖励500个赏金点,这个答案严格地解决了我在ECMAScript 2015及其中"超级分类"上述内置函数的问题(其中Object.setPrototypeOf()介绍了草案).

我不打算支持ECMAScript 5.1及更低版本,因为修改内置的继承链只能通过访问来实现__proto__,这不是任何 ECMAScript规范的一部分,因此依赖于实现.

PS我完全清楚不鼓励这样做的原因,这就是为什么我想确定规范是否允许"超级分类"而不是"打破网络",正如TC39喜欢说的那样.

推荐阅读
oDavid_仔o_880
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有