I want to implement a two-way-data-binding (like in Angular or Vue) using vanilla JavaScript.
The view to model part I can use add input event listener, and the model to view part, I want use Object.defineProperty's set function.
In defineProperty's set function I need to change the view's value and set the property value, but it will cause "Maximum call stack size exceeded",
because the set property value
will recursively run again and again.
Now the question is: Is there a way I can both use set function and set its property value at the same time?
Now my code :
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>2 Way Data Binding</title>
</head>
<body>
text: <input id="text" class="text" type="text">
</body>
<script type="text/javascript">
var input = document.querySelector("#text");
var data = {};
Object.defineProperty(data, "text", {
get: function(){
return input.value
},
set: function(newValue){
input.value = newValue;
// data.text = newValue; // <------ this is the problem
}
})
input.input = function(){
data.text = data.text;
}
</script>
</html>