Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用toString()进行Javascript字符串比较时会出现意外行为_Javascript_Ecmascript 6_Babeljs_Immutable.js - Fatal编程技术网

使用toString()进行Javascript字符串比较时会出现意外行为

使用toString()进行Javascript字符串比较时会出现意外行为,javascript,ecmascript-6,babeljs,immutable.js,Javascript,Ecmascript 6,Babeljs,Immutable.js,我正在尝试编写一个代码,重写Immutable.Record的“isEmpty” 无论如何,我对字符串比较有一个问题 import Immutable from 'immutable' export default function createSchema(schema) { const record = Immutable.Record(schema) const Schema = class extends record { isEmpty() { cons

我正在尝试编写一个代码,重写Immutable.Record的“isEmpty”

无论如何,我对字符串比较有一个问题

import Immutable from 'immutable'

export default function createSchema(schema) {
  const record = Immutable.Record(schema)

  const Schema = class extends record {
    isEmpty() {
      const aString = this.toString()
      const bString = this.constructor().toString()
      console.log('comp1', this.toString() === this.constructor().toString())
      console.log('comp2', aString === bString)
    }
  }

  return Schema
}
在以下代码中

import createSchema from 'there'

const aRecord = createSchema({ id: '' })
const a = new aRecord({ id: '1' })
a.isEmpty()
我认为“comp1”和“comp2”都应该打印为false。(a不是空的!)

但只有“comp1”为真,“comp2”为假

怎么了

==========已添加=======

感谢@Felix Kling,我尝试了“新”关键字

我发现换一行很不合逻辑

const bString = new this.constructor().toString()
使comp1为假

但是,


不会使comp1为false

成为一个一般性的观察结果:您可能想要执行新的this.constructor()。我必须是盲人。。。代码与
a=newa
的关系如何-我看不出
a
是在哪里定义的,所以…哦哇。。。问题完全变了,不是吗@JaromandaX是打字错误。。。A是aRecord@Luan-问题在一段时间内发生了显著变化:p
console.log('comp1', this.toString() === new this.constructor().toString())