「React」ES6クラスのサンプル
コンストラクタの形式
class クラス名 {
constructor(name) {
this.変数名 = name;
}
}
class クラス名 {
constructor(name) {
this.変数名 = name;
}
}
class クラス名 { constructor(name) { this.変数名 = name; } }
使用例
<!DOCTYPE html>
<html>
<body>
<script>
/*クラスの定義*/
class Car {
constructor(name,addr) {
this.brand = name;
this.addr = addr
}
}
/*インすテンスの生成*/
mycar = new Car("BMW","tokyo");
document.write(mycar.brand);
document.write("<br>");
document.write(mycar.addr);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<script>
/*クラスの定義*/
class Car {
constructor(name,addr) {
this.brand = name;
this.addr = addr
}
}
/*インすテンスの生成*/
mycar = new Car("BMW","tokyo");
document.write(mycar.brand);
document.write("<br>");
document.write(mycar.addr);
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> /*クラスの定義*/ class Car { constructor(name,addr) { this.brand = name; this.addr = addr } } /*インすテンスの生成*/ mycar = new Car("BMW","tokyo"); document.write(mycar.brand); document.write("<br>"); document.write(mycar.addr); </script> </body> </html>
結果
BMW
tokyo