使用toString()进行Javascript字符串比较时会出现意外行为
我正在尝试编写一个代码,重写Immutable.Record的“isEmpty” 无论如何,我对字符串比较有一个问题使用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
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())