jsとreactを勉強し始めた初心者です。
クラスの概念の理解につまづいています。
mdnから引用した、以下のclassの定義について伺いたいです。
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Classes
js
1var Polygon = class { 2 constructor(height, width) { 3 this.height = height; 4 this.width = width; 5 } 6}; 7 8// 名前つき 9var Polygon = class Porygon { 10 constructor(height, width) { 11 this.height = height; 12 this.width = width; 13 } 14};
①classとは何に使うのでしょうか?
個人的な今の感覚ですと「メソッドやプロパティの格納庫」です。
②jsで関数を定義する際は、
function ()~
や
var x = function ()~
と覚えたのですが、class内で関数を定義する際はこのようにせず、いきなり関数名から初めてよいのでしょうか??
例えば以下のコードはreactの公式チュートリアルのものですが
https://reactjs.org/tutorial/tutorial.html
constructor,
renderSquare,
render
という関数をいきなり定義しているように見えます。
(クラスの継承の話と関わりがあるのか……??)
js
1class Board extends React.Component { 2 constructor(props) { 3 super(props); 4 this.state = { 5 squares: Array(9).fill(null), 6 }; 7 } 8 9 renderSquare(i) { 10 return <Square value={i} />; 11 } 12 13 render() { 14 const status = 'Next player: X'; 15 16 return ( 17 <div> 18 <div className="status">{status}</div> 19 <div className="board-row"> 20 {this.renderSquare(0)} 21 {this.renderSquare(1)} 22 {this.renderSquare(2)} 23 </div> 24 <div className="board-row"> 25 {this.renderSquare(3)} 26 {this.renderSquare(4)} 27 {this.renderSquare(5)} 28 </div> 29 <div className="board-row"> 30 {this.renderSquare(6)} 31 {this.renderSquare(7)} 32 {this.renderSquare(8)} 33 </div> 34 </div> 35 ); 36 } 37}
③(よろしければ)上記2点、自分でググって納得できなかったのですが、もしググって解決する場合どのようなサイトを探し当て納得されているか教えていただければ幸いです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。