前提
Mozzila/JS入門の課題の解決方法で悩んでいる
初心者のためのオブジェクト指向 JavaScript
https://developer.mozilla.org/ja/docs/conflicting/Learn/JavaScript/Objects/Classes_in_JavaScript#constructors_and_object_instances
実現したいこと
コンストラクター関数の引数である配列の最後の項目の直前に文字列を挿入したい
発生している問題・エラーメッセージ
array.splice()がうまく動いていない?
該当のソースコード
JavaScript
1class Person { 2 constructor(first, last, age, gender, ...interests) { 3 this.name = { 4 first: first, 5 last: last, 6 }; 7 this.age = age; 8 this.gender = gender; 9 this.interests = interests; 10 this.bio = function () { 11 let pronoun = ""; 12 gender === "male" ? (pronoun = "He") : (pronoun = "She"); 13 14 interests.splice(-1, 0, "and"); 15 16 return `${this.name.first} ${this.name.last} is ${age} years old. ${pronoun} likes ${interests}.`; //☆ここ 17 }; 18 this.greeting = function () { 19 alert(`Hi, I'm ${this.name}.`); 20 }; 21 } 22}
this.interests.splice(ではないでしょうか?
回答1件
あなたの回答
tips
プレビュー