「React ES6」クラスのメソッドを使うサンプル
書式
class クラス名{
メソッド名(){xxx}
}
使用例
<!DOCTYPE html>
<html>
<body>
<script>
class Person {
//コンストラクタ
constructor(name) {
this.user = name;
}
//メソッド
funcA() {
return 'this is a ' + this.user;
}
}
//オブジェクトを生成
obj = new Person("test message");
document.write(obj.funcA());
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<script>
class Person {
//コンストラクタ
constructor(name) {
this.user = name;
}
//メソッド
funcA() {
return 'this is a ' + this.user;
}
}
//オブジェクトを生成
obj = new Person("test message");
document.write(obj.funcA());
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> class Person { //コンストラクタ constructor(name) { this.user = name; } //メソッド funcA() { return 'this is a ' + this.user; } } //オブジェクトを生成 obj = new Person("test message"); document.write(obj.funcA()); </script> </body> </html>