【发布时间】:2016-09-14 23:35:32
【问题描述】:
我正在使用一个 JS 库,特别是 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()))
})
【问题讨论】:
-
请在此处发布您的代码
-
@messerbill 我不相信 SO 代码 sn-ps 支持 TypeScript。至少,我无法让它工作。
-
在此处输入即可 - 您无需使用代码 sn-p 功能
标签: javascript typescript