<script>
let collection = $state([])
class Ob {
id = $state(null)
name = $state('')
count = $state(1)
constructor(id, name) {
this.id = id
this.name = name
}
toJSON() {
return {
id: this.id,
name: this.name,
count: this.count,
}
}
}
function incr(i) {
collection[i].count++
}
collection.push(new Ob(1, '✅ Class'))
collection.push({ id: 2, name: "✅ Object", count: 1 })
collection.push(new Ob(3, '✅ Class.toJSON()').toJSON())
</script>
{#each collection as col, i}
<button type="button" onclick={() => incr(i)}>+</button>
{JSON.stringify(col)}
<br>