Mithril this confusion
by Benjamin Lupton
HTML
<script src="//unpkg.com/mithril/mithril.js"></script>
<div id="object-result"></div>
<div id="instance-result"></div>
JavaScript
const ObjectComponent = {
assert () {
return (this === ObjectComponent)
? 'this IS object component'
: 'this WAS NOT object component'
},
view () {
return m('div', this.assert())
}
}
alert(ObjectComponent.assert())
m.render(document.getElementById('object-result'), ObjectComponent)
/*
class ClassComponent {
assert () {
return (this instanceof ClassComponent)
? 'this WAS a class component instance'
: 'this WAS NOT a class component instance'
}
view () {
return m('div', this.assert())
}
}
alert((new ClassComponent()).assert())
m.render(document.getElementById('class-result'), ClassComponent)
*/