JS Bin// context binding http://jsbin.com/horamup
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input class="name" value="">
<input class="email" value="">
<button class="reset">Reset</button>
<button class="save">Save</button>
<script id="jsbin-javascript">
'use strict';
var form = {
name: 'Test',
email: 'test@test.com',
reset: function reset(e) {
document.querySelector('.name').value = this.name;
document.querySelector('.email').value = this.email;
},
save: function save(e) {
this.name = document.querySelector('.name').value;
this.email = document.querySelector('.email').value;
},
toString: function toString() {
return this.name + ' <' + this.email + '>';
},
log: function log() {
console.log(this.toString());
}
};
document.querySelector('.reset').addEventListener('click', function () {
return form.reset();
});
document.querySelector('.save').addEventListener('click', function () {
return form.save();
});
setInterval(function () {
return form.log();
}, 1000);
</script>
<script id="jsbin-source-javascript" type="text/javascript">const form = {
name: 'Test',
email: 'test@test.com',
reset: function(e) {
document.querySelector('.name').value = this.name;
document.querySelector('.email').value = this.email;
},
save: function(e) {
this.name = document.querySelector('.name').value;
this.email = document.querySelector('.email').value
},
toString() {
return `${this.name} <${this.email}>`;
},
log() {
console.log(this.toString());
}
}
document.querySelector('.reset').addEventListener('click', () => form.reset());
document.querySelector('.save').addEventListener('click', () => form.save());
setInterval(() => form.log(), 1000);</script></body>
</html>
'use strict';
var form = {
name: 'Test',
email: 'test@test.com',
reset: function reset(e) {
document.querySelector('.name').value = this.name;
document.querySelector('.email').value = this.email;
},
save: function save(e) {
this.name = document.querySelector('.name').value;
this.email = document.querySelector('.email').value;
},
toString: function toString() {
return this.name + ' <' + this.email + '>';
},
log: function log() {
console.log(this.toString());
}
};
document.querySelector('.reset').addEventListener('click', function () {
return form.reset();
});
document.querySelector('.save').addEventListener('click', function () {
return form.save();
});
setInterval(function () {
return form.log();
}, 1000);