<!DOCTYPE html>
<html>
<body>
<h2>JavaScript のゲッターとセッター</h2>
<p>カウンターの作成に最適</p>
<p id="demo"></p>
<script>
// オブジェクトを定義する
const obj = {counter : 0};
// セッターとゲッターを定義する
Object.defineProperty(obj, "reset", {
get : function () {this.counter = 0;}
});
Object.defineProperty(obj, "increment", {
get : function () {this.counter++;}
});
Object.defineProperty(obj, "decrement", {
get : function () {this.counter--;}
});
Object.defineProperty(obj, "add", {
set : function (value) {this.counter += value;}