Two-way binding
2022年5月1日小于 1 分钟
Two-way binding
Question
Let's do some simple two-way binding.
Please create a function model(state, element)
, to bind state.value
to the HTMLInputElement element
.
const input = document.createElement('input')
const state = { value: 'BFE' }
model(state, input)
console.log(input.value) // 'BFE'
state.value = 'dev'
console.log(input.value) // 'dev'
input.value = 'BFE.dev'
input.dispatchEvent(new Event('change'))
console.log(state.value) // 'BFE.dev'
Code
/**
* @param {{value: string}} state
* @param {HTMLInputElement} element
*/
function model(state, element) {
// your code here
}