teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

全角空白があるようなので、再度コピーペーストしてみました。

2019/10/06 14:16

投稿

Chirnil
Chirnil

スコア15

title CHANGED
File without changes
body CHANGED
@@ -65,11 +65,13 @@
65
65
  addItem(item) {
66
66
  this.#items.push(item);
67
67
  }
68
+
68
69
  showItems() {
69
- console.log(this.#items);
70
+ for (item of this.#items){
71
+ console.log(thing + "<br >");
70
- }
72
+ }
71
-
72
73
  }
74
+ }
73
75
 
74
76
  class Hero extends Creature {
75
77
  #name='';
@@ -91,14 +93,12 @@
91
93
 
92
94
  toString() {
93
95
  return `Your hero, ${this.name} has ${this.health} in health.<br>
94
- He is carrying a backpack that contains, ${this.myBackpack}`;   
96
+ He is carrying a backpack that contains ${this.myBackpack}`;
95
97
  }
96
98
  }
97
-
98
99
  let hero = new Hero (500, "Christian");
99
100
  hero.myBackpack.addItem(new Item("Flask of Water", 12), new Item("Apple", 0.3));
100
101
  console.log(hero.toString());
101
-
102
102
  ```
103
103
  このコードを実行すると、
104
104
  Your hero, Christian has 500 in health.<br>

1

このコードを実行した結果と、問題点の詳細を追加しました。

2019/10/06 14:16

投稿

Chirnil
Chirnil

スコア15

title CHANGED
File without changes
body CHANGED
@@ -19,7 +19,7 @@
19
19
  Christiannのhealth pointは500
20
20
  です。
21
21
 
22
- Backpack内のコードが特よくわせん
22
+ Backpack内のコードに問題があるのではないと思ってい
23
23
  Items内に、どんどんitemを追加していけるようにしたいです。(addItem)
24
24
  Items内のitemを表示させたいです。(showItems)
25
25
 
@@ -37,7 +37,6 @@
37
37
  return this.#health;
38
38
  }
39
39
  }
40
-
41
40
  class Item {
42
41
 
43
42
  #itemName = '';
@@ -54,28 +53,24 @@
54
53
  return this.#weight;
55
54
  }
56
55
  }
56
+
57
57
  class Backpack {
58
58
 
59
59
  #items = [];
60
60
 
61
+ get items(){
62
+ return this.#items;
63
+ }
64
+
61
- addItem(item) {     
65
+ addItem(item) {
62
- #items.push(item);   //ここが合っているのか、よくわかりません。
66
+ this.#items.push(item);
63
67
  }
64
-
65
-
66
- showItems(items) {
68
+ showItems() {
67
- for (let thing in #items) {    //Itemsに入ってるものを一個ずつ表示させたいです。
68
- return console.log(thing);   
69
+ console.log(this.#items);
69
70
  }
70
- }
71
-
72
- get items(){
73
- return #items;
74
- }
75
71
 
76
72
  }
77
73
 
78
-
79
74
  class Hero extends Creature {
80
75
  #name='';
81
76
  #myBackpack = '';
@@ -96,14 +91,20 @@
96
91
 
97
92
  toString() {
98
93
  return `Your hero, ${this.name} has ${this.health} in health.<br>
99
- He is carrying a backpack that contains, ${this.myBackpack}`;    //この書き方でいいのでしょうか_
94
+ He is carrying a backpack that contains, ${this.myBackpack}`;   
100
95
  }
101
96
  }
102
97
 
103
- //ここの書き方(呼び出し方)もよくわかっていません。
104
98
  let hero = new Hero (500, "Christian");
105
- hero.myCoolBackpack.addItem(new Item("Flask of Water", 12), new Item("Apple", 0.3));
99
+ hero.myBackpack.addItem(new Item("Flask of Water", 12), new Item("Apple", 0.3));
100
+ console.log(hero.toString());
106
101
 
107
102
  ```
108
-
103
+ このコードを実行すると、
104
+ Your hero, Christian has 500 in health.<br>
105
+ He is carrying a backpack that contains, [object Object]
106
+ となり、itemがうまく表示されません。この[object,object]を
107
+ **Flask of Water, weight: 1.2**
108
+ **Apple, weight: 0.3**
109
+ とするにはどのような変更が必要なのでしょうか?
109
110
  アドバイスをどうかよろしくお願いします。