index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="MyClass.js"></script>
<title>クラスを作ってみる1</title>
</head>
<body>
<h1>クラスを作ってみる1</h1>
<script>
let shohin = [];
shohin[0]= new MyClass("A", 10, "iPhone9", 65000);
shohin[1] = new MyClass("A", 0, "iPhoneX", 105000);
shohin[2] = new MyClass("A", 5, "Pixel3", 64000);
shohin[3] = new MyClass("A", 20, "Pixel4", 75000);
shohin[4] = new MyClass("A", 8, "Oppo7", 25000);
shohin[5] = new MyClass("A", 0, "HuaweiP10", 17000);
shohin[6] = new MyClass("A", 11, "Galaxy4", 105000);
shohin[7] = new MyClass("A", 0, "AquosLite", 45000);
shohin[8] = new MyClass("A", 3, "Blackberry", 42000);
shohin[9] = new MyClass("A", 1, "iPad", 40000);
// 全商品表示
for(let i in shohin){ // for(let i=0; i<10; i++){
shohin[i].show();
}
// 在庫チェック
for(let i=0; i<shohin.length; i++){
shohin[i].zaikoCheck();
}
</script>
</body>
</html>
MyClass.js
/* MyClass.js : クラス */
class MyClass{
constructor(kubun, zaiko, name, tanka){
this.kubun = kubun;
this.zaiko = zaiko;
this.name = name;
this.tanka = tanka;
}
show(){ // 商品情報表示
console.log(this.kubun);
console.log(this.name);
console.log(this.tanka);
console.log(this.zaiko);
}
zaikoCheck(){ // 在庫チェック
if(this.zaiko <= 0){
console.log(this.name + " 在庫がありません");
}
}
}


コメント