æ¬æ¥ã®ãã¥ã¼ããªã¢ã«ã¯JavaScriptã®ã¯ã©ã¹ã«ã¤ãã¦å¦ç¿ãã¾ãã
JavaScriptã¯ã©ã¹ãå¦ç¿ãå§ããããªãã¯ä¸ç´è ã¬ãã«ã«å°éãã¦ãã¾ãã
Reactãªã©ã®ä¸è¬çãªJavaScriptã©ã¤ãã©ãªã¯ã¯ã©ã¹æ§æãé »ç¹ã«ä½¿ç¨ãããããã¯ã©ã¹ã«ç²¾éãã¦ããã¨é常ã«å½¹ç«ã¡ã¾ãã
æ¬æ¥ã¯ES6ã¯ã©ã¹æ§æããã³ES7ã¯ã©ã¹æ§æã®è§£èª¬ãè´ãã¾ãã
JavaScriptã«ãããã¯ã©ã¹ã¨Prototypeã®éãã¯ãä»ã®è¨äºã¨ãã¦åãæ±ãã¾ãããPrototypeã«ã¤ãã¦ç²¾éãã¦ããªãåå¿è æ§ãããã£ããããã¨æãã¾ãã®ã§çãã«è§£èª¬ãã¾ãã
ãã®è¨äºã§ã¯ããã§ã«ï¼Prototypeï¼ããã³ï¼thisï¼ã«ããç¨åº¦ç²¾éãã¦ããæ¹ã対象ã¨ãã¦ããäºããç解ãã ããã
Prototypeã¨ã¯ï¼
JavaScriptã«ãããprototypeã¯ããããªãã¸ã§ã¯ãã®ããããã£ãã¡ã½ãããä»ã®ãªãã¸ã§ã¯ãããç¶æ¿ããããã®ä»çµã¿ã§ãã
ãã¹ã¦ã®JavaScriptã®ãªãã¸ã§ã¯ãã¯ã prototypeã¨ããç¹å¥ãªããããã£ãæã£ã¦ãã¾ãã
ãã®prototypeã¯ããªãã¸ã§ã¯ãã®ãå ãã¨ãªãå¥ã®ãªãã¸ã§ã¯ããæãã¾ãã
ãã®ãå ãã¨ãªããªãã¸ã§ã¯ãã«ã¯ãããããã£ãã¡ã½ãããå®ç¾©ããã¦ãããã¨ãããã¾ãã
ä¾ãã°ãä¸è¨ã®ã³ã¼ããã確èªä¸ããã
Person
ãªãã¸ã§ã¯ãã®prototypeã«sayHello
ã¡ã½ãããå®ç¾©ããã¾ãã
function Person(name, age) { this.name = name; this.age = age; } Person.prototype.sayHello = function() { console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`); }; const taro = new Person("Taro", 30); taro.sayHello(); // "Hello, my name is Taro and I'm 30 years old."
Person
ã³ã³ã¹ãã©ã¯ã¿ã¼é¢æ°ã®prototypeã« sayHello
ã¡ã½ãããå®ç¾©ããããã¨ã§ãtaro
ãªãã¸ã§ã¯ãã sayHello
ã¡ã½ãããç¶æ¿ãããã¨ãã§ãã¾ãã
ã¤ã¾ãã taro.sayHello()
ã¨ããã³ã¼ããå®è¡ããã¨ã Person
ã®prototype ã«å®ç¾©ãããsayHello
ã¡ã½ãããå¼ã³åºããã¾ãã
prototypeã¯ããªãã¸ã§ã¯ãã®ããããã£ãã¡ã½ãããç¶æ¿ããããã®ä¸è¬çãªæ¹æ³ã§ãããJavaScriptã§ã®ãªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ã«ããã¦éè¦ãªæ¦å¿µã¨ãªã£ã¦ããã¾ãã
ã¯ã©ã¹ã¨ã¯ï¼
JavaScriptã¯ã©ã¹ã¯ECMAScript2015(ES6)ã§å°å ¥ããã¾ããã
ES6以åã§ã¯ãã¯ã©ã¹ã®æ¦å¿µããªãã£ãã®ã§é¢æ°ã使ç¨ããããã¿ã¤ãã®æ¹æ³ã§å®è£ ãã¦ãã¾ããã
ã¯ã©ã¹ã¯ãä»ã®ããã°ã©ãã³ã°è¨èªã¨ã¯ç°ãªããJavaScriptã®ã·ã³ã¿ãã¯ã¹ã·ã¥ã¬ã¼ã§ãã
JavaScriptã«ã¯é©åãªã¯ã©ã¹ãå«ã¾ãã¦ãã¾ããã
ã¯ã©ã¹ã¯ãé¢æ°ã§ã®ãããã¿ã¤ãã使ç¨ãã以åã§ã®ã¡ã½ãããç½®ãæãããªãã¸ã§ã¯ããã³ãã¬ã¼ããä½æããããã«æ¹åãããããã¯ãªã¼ã³ãªæ§æãæä¾ããããã®JavaScriptã®ã¯ã©ã¹æ¦å¿µã¨ãªã£ã¦ããã¾ãã
ã¤ã¾ããJavaScriptã®ã¯ã©ã¹ã¯ãªãã¸ã§ã¯ãã§ã¯ãªããJavaScriptãªãã¸ã§ã¯ããä½æããããã®ãã³ãã¬ã¼ãã®ãããªãã®ã§ãã
JavaScriptã«ãããã¯ã©ã¹ã¯ããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ã«ãããéè¦ãªæ¦å¿µã®ä¸ã¤ã§ãã
ããã§ã¯ãJavaScriptã¯ã©ã¹ã®åºæ¬çãªä½¿ç¨æ¹æ³ã説æãã¾ãã
JavaScriptã¯ã©ã¹æ§æ
ã¯ã©ã¹ã¯ç¹æ®ãªç¨®é¡ã®é¢æ°ãªã®ã§ãé¢æ°å®£è¨ãé¢æ°å¼(ç¡åé¢æ°)ã¨åãããã«ã¯ã©ã¹ãå®ç¾©ã§ãã¾ãã
ã¯ã©ã¹ã¯é¢æ°ã«ä¼¼ã¦ãã¾ãã
ã» ã¯ã©ã¹é¢æ°å®£è¨(ååä»ã)
ã» ã¯ã©ã¹é¢æ°å¼(ç¡åé¢æ°)
ãããã¯classãã¼ã¯ã¼ãã«ãã£ã¦å®£è¨ããã¾ãã
JavaScriptã¯ãã¯ã©ã¹ãå®ç¾©ãã主è¦ãªæ¹æ³ã§ããclassãã¼ã¯ã¼ããæä¾ãã¾ãã
ããã¯ãæ¢åã®ãããã¿ã¤ãç¶æ¿ãã¿ã¼ã³ã«å¯¾ããã·ã³ã¿ãã¯ã¹ã·ã¥ã¬ã¼ã¨ãã¦æ©è½ãã¾ãã
JavaScriptã¯ã©ã¹ãä½æããã«ã¯ãclassãã¼ã¯ã¼ãã®å¾ã«ââã¯ã©ã¹ã®ååãç¶ãã¾ãã
以ä¸ã®Classname
ã¯ã¯ã©ã¹ã®ååã§ããã¯ã©ã¹åã¯é常ã§ã¯ã¯ã©ã¹åã®å
é ã«å¤§æåã使ç¨ãã¦å®ç¾©ããã¾ãã
// ã¯ã©ã¹å®£è¨ class Classname { { class æ¬ä½ } }
以ä¸ã§ã®newã¯ã¯ã©ã¹ãã¤ã³ã¹ã¿ã³ã¹åãã¾ãã
æ°ãã空ã®ãªãã¸ã§ã¯ããä½æãããthisã«å²ãå½ã¦ããã¾ãã
const Classname = new Classname();
Classname
ã¯ã©ã¹ã®æ°ãããªãã¸ã§ã¯ãã§ããã¤ã³ã¹ã¿ã³ã¹ãä½æããæ¹æ³ã§ãã
console.log(typeof Classname); // function
ã¤ã³ã¹ã¿ã³ã¹ã«é¢ãã¾ãã¦ã¯ãå¾ã«è©³ãã解説è´ãã¾ãã
JavaScriptã§ã¯ãã¯ã©ã¹ã¯ä¸ç¨®ã®é¢æ°ã¨ãªã£ã¦ãã¾ãã
ããªããããã¯ã©ã¹ã«ååãä»ããå¿ è¦ã¯ããã¾ããã
ã¯ã©ã¹å¼ã使ç¨ããã¨ãã¯ã©ã¹ãå¤æ°ã«å²ãå½ã¦ããã¨ãã§ãã¾ãã
以ä¸ã§ã¯ãç¡åé¢æ°ã§ããååç¡ãã®å®£è¨ã¨ãªãã¾ãã
const User = class {} // unnamed const User = class User {} // named
ãããã®ããã«ãã¯ã©ã¹ã¯ååä»ãã¾ãã¯ååãªãã«ãããã¨ãã§ãã¾ãã
ååä»ãã¯ã©ã¹å¼ã«ä»ããããååã¯ãã¯ã©ã¹ã®æ¬ä½ã«å¯¾ãã¦ãã¼ã«ã«ã§ãã
ååä»ãã¯ã©ã¹ã¨ç¡åã¯ã©ã¹ã®æãéè¦ãªéãã¯ãååä»ãã¯ã©ã¹ãåå©ç¨å¯è½ã§ãããã¨ã§ãã
ååä»ãã¯ã©ã¹ã¯ãè¤æ°ã®å ´æã§åãååã§åç §ã§ãã¾ãã
ä¸æ¹ã§ãç¡åã¯ã©ã¹ã¯ããã®ã¤ã³ã¹ã¿ã³ã¹ãä½æããããã«ã®ã¿ä½¿ç¨ãããåå©ç¨ãããã¨ã¯ã§ãã¾ããã
åæå
ã¯ã©ã¹ã¯é¢æ°ã®ä¸ç¨®ã§ããä¼¼ã¦ããã¾ãããå®éã«ã¯é¢æ°ã¨ã¯å¤å°ç°ãªãé¨åãããã¾ããããã¯JSã®ã¯ã©ã¹ã¯å·»ãä¸ãããã¾ããã
JavaScriptã®ã¯ã©ã¹ã«ã¯ãconstructorã¨å¼ã°ããç¹å¥ãªã¡ã½ãããããããªãã¸ã§ã¯ãã®åæåæã«ãã£ã¼ã«ãã®åæå¤ãè¨å®ã§ãã¾ãã
ã¤ã¾ããconstructorã¯ãªãã¸ã§ã¯ãã®ä½æã¨ããããã£ãåæåããããã«ä½¿ç¨ãããé¢æ°ãªãã¸ã§ã¯ãã§ãã
æå®ãããã¯ã©ã¹ã®æ°ãããªãã¸ã§ã¯ããä½æãããã¨ãèªåçã«å®è¡ããã¾ãã constructoré¢æ°ã¯ãé¢æ°èªä½ãåç
§ãthis
ã®ããããã£ã¨ãã¦å²ãå½ã¦ãããããã¤ãã®ãã©ã¡ã¼ã¿ã¼ã§åæåããã¾ãã
class User { constructor(name, age){ this.name = name this.age = age } }
å¼æ°ã¯æ¬å¼§ï¼ï¼ã§å²ã¾ãããã®å¾ã«ä¸æ¬å¼§ï½ï½ã§å²ã¾ããã³ã¼ããããã¯ãç¶ãã¾ãã
ãããã®ä¸æ¬å¼§ã¯ãã¡ã½ããæ¬ä½ã®éå§ä½ç½®ã®éãä¸æ¬å¼§ï½ã¨çµäºä½ç½®ã®éãä¸æ¬å¼§ï½ã示ãã¾ãã
å¤å´ã«ãããã¢ã®ä¸æ¬å¼§ã¯ãã¯ã©ã¹èªä½ãã®ãã®ãå®ç¾©ããå å´ã®ãã¢ã®ä¸æ¬å¼§ã¯constructorã¡ã½ããã®å¢çãå®ç¾©ããããã«ä½¿ç¨ããã¦ãã¾ãã
thisãã¼ã¯ã¼ãã使ç¨ãã¦ç¾å¨ã®ã¯ã©ã¹ãåç §ãã¾ãã
ã¾ããconstructorã¡ã½ããã¯1ã¤ããåå¨ã§ãã¾ããã
thisãã¼ã¯ã¼ãã«ç²¾éããã¦ããªãæ¹ã¯ãå½ããã°ã®ä»¥ä¸ã®ãã¥ã¼ããªã¢ã«ãåç §ãã¦ã¿ã¦ä¸ããã
ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹å
ã¤ã³ã¹ã¿ã³ã¹ã¨ã¯ãã¯ã©ã¹ã®ããããã£åã¨ã¡ã½ãããå«ãããä¸æã®ããããã£å¤ãæã¤ãªãã¸ã§ã¯ãã§ãã
ã¤ã¾ããã¤ã³ã¹ã¿ã³ã¹ã¯ã¯ã©ã¹ã«ãã£ã¦è¨è¿°ããããã¼ã¿ã¨åä½ãå«ããªãã¸ã§ã¯ãã¨ããäºã§ãã
ä¾ãã°ãã¯ã©ã¹ã¯ã«ãã´ãªã¨èãããã¨ãã§ãã¾ãã
åã¯ã©ã¹ã¯ç°ãªãå¤ãå«ã¿ãã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹(ãªãã¸ã§ã¯ã)ã®ç¶æ
ãè¨è¿°ããå±æ§
ãããããã£
ãæã¤ãã¨ãã§ãã¾ãã
åã¦ã¼ã¶ã¼æ å ±ã®ç°ãªãå¤ã¨èãã¦ã¿ã¦ä¸ããã
ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãä½æããã¨ã¯ãããããã¯ã©ã¹ã¨ãã¦ç®¡çããã®ãã¤ã³ã¹ã¿ã³ã¹ã«ãªãã¾ãã
ã¯ã©ã¹ã使ã£ã¦ãªãã¸ã§ã¯ããä½æããã«ã¯ãnewãã¼ã¯ã¼ãã使ç¨ãã¾ãã
class User { constructor(name, age){ this.name = name this.age = age } } const user = new User('Taro', 30); document.write("åºå:" + " " + user.name + " " + user.age);
ä¸è¨ã®ã³ã¼ãã§ã¯ãnewãã¼ã¯ã¼ãã使ç¨ãã¦User
ã¯ã©ã¹ããuser
ã¨ããã¤ã³ã¹ã¿ã³ã¹ãä½æãã¦ãã¾ãã
this.name = name
ã¨ããå¼ã§ããã£ã¼ã«ãã®name
ãä½æããåæå¤ã代å
¥ãã¦ãã¾ãã
User
ã¯ã©ã¹ã®constructorã¡ã½ãããå¼ã³åºãããname
ããããã£ã«"Taro"
ãage
ããããã£ã«30
ãè¨å®ããã¾ãã
ããã§ãæ°ããuser
ãªãã¸ã§ã¯ããä½æãããã®name
ããããã£ã«ã¢ã¯ã»ã¹ã§ããããã«ãªãã¾ããã
See the Pen JavaScript ã¯ã©ã¹å®ç¾© åæå by dev.K | Webã¢ããªéçºè (@enjinia_f) on CodePen.
ã¯ã©ã¹ã®ã¡ã½ãã
ã¯ã©ã¹ã¯ãã¡ã½ããã追å ããããã®ããã¯ãªã¼ã³ã§æ´ç·´ãããæ§æãæä¾ãã¦ãã¾ãã
class User { constructor(name) { this.name = name; } getName() { return this.name; } }
this
ãã¼ã¯ã¼ãã使ç¨ããã¨ãconstructorã¨ã¡ã½ããå
ã®ã¤ã³ã¹ã¿ã³ã¹ãã¼ã¿ã«ã¢ã¯ã»ã¹ã§ãã¾ãã
const user = new User('Taro'); console.log(user.getName()); // Taro
ã¡ã½ããå¼ã³åºãã®user.getName()
ã¯ãUserã¯ã©ã¹å
ã®ã¡ã½ãããå®è¡ããname
ããããã£ãè¿ãã¾ãã
See the Pen JavaScript ã¯ã©ã¹ ã¡ã½ãã by dev.K | Webã¢ããªéçºè (@enjinia_f) on CodePen.
ä»ã®ä¾ã§ãããå°ãå¦ãã§ã¿ã¾ãããã
class Person { constructor(name, age) { this.name = name; this.age = age; } sayHello() { console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`); } }
ä¸è¨ã®ã³ã¼ãã§ã¯ãPerson
ã¨ããã¯ã©ã¹ãå®ç¾©ãã¦ããã¾ãã
constructorã¡ã½ããã¯ãã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãä½æãããã¨ãã«å¼ã³åºãããç¹å¥ãªã¡ã½ããã§ãname
ã¨age
ã¨ãã2ã¤ã®å¼æ°ãåãåã£ã¦ãããããthis.name
ã¨this.age
ã¨ããããããã£ã«å¤ãè¨å®ãã¦ãã¾ãã
sayHello
ã¡ã½ããã¯ãthis.name
ã¨this.age
ã®å¤ã使ã£ã¦ãæ¨æ¶ãããããã®ã¡ã½ããã¨ãªãã¾ãã
éçã¡ã½ãã
ã¡ã½ããããã®ãããã¿ã¤ãã§ã¯ãªããã¯ã©ã¹é¢æ°èªä½ã«å²ãå½ã¦ããã¨ãå¯è½ã§ãã
ãã®ãããªã¡ã½ããã¯staticï¼éçï¼ã¨å¼ã°ãã以ä¸ã®ãã¼ã¯ã¼ããå é ã«è¿½å ããã¾ãã
class Double { static double(n) { return n * 2; } } Double.double(2); // 4
ä¸è¨ã®Doubleã¯ã©ã¹ã®doubleã¡ã½ããã¯ãä¸ããããæ°å¤ã2åã«ããåç´ãªè¨ç®ãè¡ã£ã¦ãã¾ãã
éçã¡ã½ããã§ã¯ãã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ç¹æã®ãã¼ã¿ãç¶æ ã«ã¢ã¯ã»ã¹ãããã¨ã¯ã§ãã¾ãããããã®ä»£ããã«å¼æ°ãåãåã£ã¦å¦çãè¡ãã¾ãã
ãã®ãããªç°¡åãªè¨ç®ãå¦çãè¡ãå ´åãã¤ã³ã¹ã¿ã³ã¹ãä½æããå¿ è¦ã¯ãªããã¯ã©ã¹èªä½ã§ç´æ¥ã¡ã½ãããå¼ã³åºããã¨ãã§ãããããéçã¡ã½ãããæç¨ã§ãã
ãã®ããã«ãéçã¡ã½ããã¯ã¦ã¼ãã£ãªãã£ã¯ã©ã¹ã§è¨ç®ãè¡ãããã«ãã使ç¨ããã¾ãã
ã¯ã©ã¹ã®æ¡å¼µããã³ç¶æ¿
extends
ãã¼ã¯ã¼ãã使ç¨ãã¦ãJavaScriptã®ãªãã¸ã§ã¯ãã¨ã¯ã©ã¹ãæ¡å¼µã§ãã¾ãã
â» ä¸é¨ã®ã¯ã©ã¹ãæ¡å¼µããã¯ã©ã¹ã¯åã¯ã©ã¹ã¨å¼ã°ããä»ã®ã¯ã©ã¹ã¯è¦ªã¯ã©ã¹ã¨å¼ã°ãã¾ãã
é常ã§ã¯ãå¥ã®ã¯ã©ã¹ã®åã§ããã¯ã©ã¹ãä½æããããã«ä½¿ç¨ããã¾ãã
ã¯ã©ã¹ãæ¡å¼µããçç±ã¯ãæ°ããã¯ã©ã¹ã«å ã®ã¯ã©ã¹ã¨åãæ©è½ããã¹ã¦æããããå ´åã¯ãã¯ã©ã¹ãæ¡å¼µãã¾ãã
åã¯ã©ã¹ã¯ãæ°ããæ©è½ã追å ãããã親ã¯ã©ã¹ã®ããã¤ãã®æ©è½ããªã¼ãã¼ã©ã¤ããããã¨ãã§ãã¾ãã
ã¤ã¾ããextendsãã¼ã¯ã¼ãã¯ãä»ã®ã¯ã©ã¹ã¾ãã¯è¦ªã¯ã©ã¹ã«åå¨ããããããã£ã¨ã¡ã½ããã«ã¢ã¯ã»ã¹ããã®ã«å½¹ç«ã¡ã¾ãã
ES6ã§ã¯ã©ã¹ãæ¡å¼µããããã»ã¹ã¯ç°¡åã§ãã
ã¯ã©ã¹åã®å¾ã«extendsãã¼ã¯ã¼ããè¨è¿°ããæ¡å¼µããã¯ã©ã¹ã®ååãè¨è¿°ããã ãã§ãã
class ChildClass extends ParentClass{ // å®ç¾© }
ä¾ãã°ã以ä¸ã®ãããªã¯ã©ã¹Person
ãããã¨ãã¾ãã
class Person { personInfo() { //... } }
ãã®ã¯ã©ã¹ãæ¡å¼µãå½¢æãããã¼ã¹ã¯ã©ã¹ããæ¡å¼µãã¦personInfo()
ã¡ã½ãããç¶æ¿ããç¹å®ã®ã¡ã½ãããããããã£ãæä¾ã§ãã¾ãã
class Student extends Person { studentInfo() { //... } }
ES6ã§ã¯extendsã¨superãã¼ã¯ã¼ãã使ç¨ãããã¨ã§ã以åã®ãããã¿ã¤ãã®ç¶æ¿ææ³ã®æé ãç°¡ç¥åãã¾ããã
以ä¸ã®ä¾ã§ã¯ãPerson
ã¯ã©ã¹ã¨Student
ã¯ã©ã¹ãå®ç¾©ããextendsã¨superãã¼ã¯ã¼ãã§ç¶æ¿ã確ç«ãã¦ãã¾ãã
class Person { constructor(name) { this.name = name; } personInfo() { console.log("çå¾ã®åå: " + this.name); } } class Student extends Person { constructor(name) { super(name); } studentInfo() { console.log("親ã¯ã©ã¹ã®çå¾ã®åå: " + this.name); } } let student = new Student("Taro"); student.personInfo(); student.studentInfo();
See the Pen JavaScript ã¯ã©ã¹ æ¡å¼µ by dev.K | Webã¢ããªéçºè (@enjinia_f) on CodePen.
ã¾ããextendsãã¼ã¯ã¼ãã使ç¨ããPerson
ã¯ã©ã¹ãç¶æ¿ããStudent
ã¯ã©ã¹ã«ãã¾ãã
//親ã¯ã©ã¹ class Person { // ... } //åã¯ã©ã¹ class Student extends Person { // ... }
å
è¿°ããããã«ãä¸è¨ã§ããã°Person
ã¯ã©ã¹ã¯åºæ¬ã¯ã©ã¹ããã³è¦ªã¯ã©ã¹å¼ã°ããStudent
ã¯ã©ã¹ã¯æ´¾çã¯ã©ã¹ã¾ãã¯åã¯ã©ã¹ã¨å¼ã°ãã¦ãã¾ãã
Student
ã¯ã©ã¹ã¯ãPerson
ã¯ã©ã¹ã®ãã¹ã¦ã®ããããã£ã¨ã¡ã½ãããç¶æ¿ãã¾ãã
ããã¦ãStudent
ã¯ã©ã¹ã®constructorã§super()ãå¼ã³åºãname
ãå¼æ°ã¨ãã¦Person
ã®constructorãå¼ã³åºãã¾ãã
class Student extends Person { constructor(name) { super(name); } }
JavaScriptã§ã¯ãåã¯ã©ã¹ãconstructorãæã¤å ´åã¯super()ãå¼ã³åºããã¨ãè¦æ±ããã¾ãã
super
ã¯è¦ªã¯ã©ã¹ããã¯ã©ã¹ãæ¡å¼µããå ´åãåã¯ã©ã¹ã使ç¨ããåã«è¦ªã¯ã©ã¹ãå¼ã³åºãå¿
è¦ãããã¾ãã
JavaScriptã«ãã£ã¦æä¾ãããsuper()ã¡ã½ãããããã®ã§ããããå¼ã³åºããè¡ãã¾ãã
ã¯ã©ã¹ã«constructorããªãå ´åã«ã¯ãä»ã«ä½ãããå¿ è¦ã¯ããã¾ããã
super()ã¯thisãªãã¸ã§ã¯ããåæåãããããthisãªãã¸ã§ã¯ãã«ã¢ã¯ã»ã¹ããåã«super()ãå¼ã³åºãå¿ è¦ãããã¾ãã
ãããã£ã¦ãsuper()ãå¼ã³åºãåã«thisã«ã¢ã¯ã»ã¹ãããã¨ããã¨ãã¨ã©ã¼ã«ãªãã¾ãã®ã§æ³¨æãã¦ä¸ããã
ES7 ã¯ã©ã¹æ§æ
ECMAScript 7(2016)ã¯ã©ã¹æ§æã®æãè¯ãç¹ã¯ãconstructorã¾ãã¯super()ã¡ã½ãããè¨è¿°ããå¿ è¦ããªããã¨ã§ãã
ããã¯ãã¢ãã¼é¢æ°ãã¯ã©ã¹ã¡ã½ããã¨ãã¦ä½¿ç¨ãããã¨ãã§ãã¾ãã
ããã«ãããã³ã¼ãã®è¨è¿°ãããç°¡åãã¤è¿ éã«ãªãã¾ãã
class Person { firstName = "Taro"; personInfo = () => { console.log(`My name is ${this.firstName}`); document.write(`My name is ${this.firstName}`); }; } const person = new Person(); person.personInfo();
ES7ã¯ã©ã¹æ§æã§ã¯ãconstructorã¨super()ã¡ã½ããã¯è£å´ã§æ¸ããã¦ããã®ã§ãããªããæ¸ãå¿ è¦ã¯ããã¾ããã
class Person { firstName = "Taro"; personInfo = () => { console.log(`My name is ${this.firstName}`); document.write(`My name is ${this.firstName}`); }; } class Student extends Person { firstName = "Taro"; lastName = "Tanaka"; printList = () => { `My name is ${this.firstName} ${this.lastName} `; }; } const personList = new Student(); console.log(personList); console.log(personList.printList());
See the Pen JavaScript ES7 ã¯ã©ã¹ by dev.K | Webã¢ããªéçºè (@enjinia_f) on CodePen.
æå¾ã«
åã種é¡ã®ãªãã¸ã§ã¯ããè¤æ°ä½æããå¿ è¦ãããå ´åã¯ãã¯ã©ã¹ã使ç¨ãã¾ãã
ã¯ã©ã¹ã¯ã strict modeï¼å³æ ¼ã¢ã¼ãï¼ã§å®è¡ãããå·»ãä¸ããããªãç¹å¥ãªç¨®é¡ã®é¢æ°ã¨ãªãã¾ãã
ã¯ã©ã¹ã«ã¯ãã¯ã©ã¹ã¤ã³ã¹ã¿ã³ã¹ããã¢ã¯ã»ã¹ã§ãããã£ã¼ã«ãã¨ã¡ã½ãããå«ãããã¨ãã§ããããããæ¡å¼µãããã¨ã«ãããä»ã®ã¯ã©ã¹ããæ©è½ãç¶æ¿ã§ãã¾ãã
åã¯ã©ã¹ã¯è¦ªã¯ã©ã¹ã®ãã¹ã¦ã®æ©è½ãç¶æ¿ã§ãããããã³ã¼ãã®åå©ç¨ãå¯è½ã«ãªãã¾ãã
ããã«ãããã³ã¼ããããã¯ãªã¼ã³ã«ãªããä¿å®ã容æã«ãªãã¾ãã
ããã ãã§ãªããåã¯ã©ã¹ã«ç¬èªã®æ©è½ã追å ãããã¨ãã§ãããããæç¨ãªæ©è½ã ããç¶æ¿ãããã®ä»ã®å¿ è¦ãªæ©è½ãå®ç¾©ãããã¨ãã§ãã¾ãã
ç¶æ¿ã¯ããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ã®ä¸å¿çãªæ¦å¿µã®ï¼ã¤ã§ãããç¶æ¿ã¨ã¯è¦ªã¯ã©ã¹ãåºæ¬ã¯ã©ã¹ã®ã¡ã½ãããå±æ§ãåã¯ã©ã¹ãæ´¾çã¯ã©ã¹ã«ã³ãã¼ã¾ãã¯ç¶æ¿ãããã¨ã§ãããJavaScriptã§ã¯extends
ã¨ãããã¼ã¯ã¼ãã使ã£ã¦å®ç¾ãããã¨ãã§ãã¾ãã
ãããã¿ã¤ãã®ç¶æ¿ã¯æ代é ãã§è¤éã§ãã
extendsã¯ããåç´ã§ãJava
ãC++
ãªã©ã®ä»ã®è¨èªã«ä¼¼ã¦ãã¾ãã
æ¬æ¥ã¯ä»¥ä¸ã¨ãªãã¾ãã
æå¾ã¾ã§ãã®è¨äºãèªãã§é ããããã¨ããããã¾ãã