javascript JS Bin //上下文绑定http://jsbin.com/horamup
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript JS Bin //上下文绑定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);
以上是关于javascript JS Bin //上下文绑定http://jsbin.com/horamup的主要内容,如果未能解决你的问题,请参考以下文章
js 基础 关于this
javascript中的this绑定
JavaScript语法——this
使用callapply和bind解决js中烦人的this,事件绑定时的this和传参问题
javascript中的this
插件中的 Vue.js 绑定上下文?