我正在使用一个JS库,具体来说是
TypeScript有没有我不知道的转换方式可以实现这一点,而不必编写自己的代码呢?
select2
,如果我传递给它的对象不是普通对象,它会表现得有些不同。通过使用jQuery的isPlainObject
函数进行检查。TypeScript有没有我不知道的转换方式可以实现这一点,而不必编写自己的代码呢?
class Opt {
constructor(public id, public text) {
}
toPlainObj(): Object {
return {
id: this.id,
text: this.text
}
}
}
let opts = [
new Opt(0, 'foo'),
new Opt(1, 'bar')
];
console.clear()
console.log('both should be false')
$.map(opts, opt => {
console.log($.isPlainObject(opt))
})
console.log('both should be true')
$.map(opts, opt => {
console.log($.isPlainObject(opt.toPlainObj()))
})