「React」ES6クラスのサンプル

コンストラクタの形式

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class クラス名 {
constructor(name) {
this.変数名 = name;
}
}
class クラス名 { constructor(name) { this.変数名 = name; } }
class クラス名 {
  constructor(name) {
    this.変数名 = name;
  }
}

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!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

React.js

Posted by arkgame