09 Class - iruma-tea/dokushujs GitHub Wiki
ã¯ã©ã¹ã®èšæ³ã¯ãã³ãŒããæŽçããããåãæ§é ã®ãªããžã§ã¯ããè€æ°äœæããããããšãã«äœ¿ãããã
ES5ãŸã§ã®JavaScriptã§ã¯ãã¯ã©ã¹ã®èšæ³ã¯ååšããªããã£ããããããES2015(ES6)ã§ã¯ã©ã¹ãè¿œå ãããããšã«ããã
JavaScriptéçºã§ãåŸã
ã«ã¯ã©ã¹ã®èšæ³ã浞éããŠããŠããããŸããES2022ã§ã¯ãã¯ã©ã¹ã®äœ¿çšãæ¡åŒµãããŠãããããã«
䟿å©ã«èšè¿°ã§ããããã«ãªã£ãŠããã
ã¯ã©ã¹ãšã¯ããªããžã§ã¯ããäœæããããã®ã²ãªåœ¢ã®ãããªãã®ã§ããããã ã¯ã©ã¹ã«ã¯ãªããžã§ã¯ãã«èšå®ãããããããã£ãã¡ãœãããå®çŸ©ã§ãããããããšã«ãªããžã§ã¯ããç°¡åã«äœæã§ããã ãŸãããªããžã§ã¯ãã®äœææã«å®è¡ãããåŠç(ã³ã³ã¹ãã©ã¯ã¿)ãå®è£ ããããšã§ãããããã£ã«èšå®ãããå€ãªã©ã ãªããžã§ã¯ãããšã«å€æŽã§ãããããã«ãããã²ãªåœ¢ã®ã¯ã©ã¹ã1ã€äœæããã ãã§ãåããããªæ§é ã®è€æ°ã®ãªããžã§ã¯ããç°¡åã«äœæã§ããã
// ãŠãŒã¶ãŒãäœæããã¯ã©ã¹ã®å®çŸ©
class User {
constructor(username, password) {
this.username = username;
this.password = password;
}
login() {
console.log(`ãã°ã€ã³[${this.username}/${this.password}]`);
}
}
const taro = new User("ç¬ç¿å€ªé", "taro-pwd"); // ãªããžã§ã¯ãã®çæïŒç¬ç¿å€ªé
const hanako = new User("ç¬ç¿è±å", "hanako-pwd"); // ãªããžã§ã¯ãã®çæïŒç¬ç¿è±å
ã¯ã©ã¹ã®å®çŸ©æ¹æ³ã¯æ¬¡ã®ããã«ãã
[æ§æ]ã¯ã©ã¹ã®èšæ³
class ã¯ã©ã¹å {
constructor([åŒæ°,....]) {
this.ãããã㣠= å€;
}
ã¡ãœãã([åŒæ°,.....]) {
}
}
**ã³ã³ã¹ãã©ã¯ã¿(constructor)**ãèšè¿°ãããã³ã³ã¹ãã©ã¯ã¿ã¯ããªããžã§ã¯ãã®çæãè¡ãããæã«å®è¡ãããé¢æ°(ã¡ãœãã)ã§ãã
ã¯ã©ã¹ãããªããžã§ã¯ããçæããåŠçãã€ã³ã¹ã¿ã³ã¹åãšåŒã³ãŸãããŸããã€ã³ã¹ã¿ã³ã¹åã«ãã£ãŠçæããããªããžã§ã¯ãã¯ãã€ã³ã¹ã¿ã³ã¹ãšåŒã³ãŸãã
ã€ã³ã¹ã¿ã³ã¹åã«ãã£ãŠã€ã³ã¹ã¿ã³ã¹ãäœæããã«ã¯ãnew æŒç®åã䜿ãã
[æ§æ] ã€ã³ã¹ã¿ã³ã¹åã«ãã£ãŠãªããžã§ã¯ããçæãã
åŒæ°ããïŒ
const ãªããžã§ã¯ã = new ã¯ã©ã¹å([åŒæ°1, åŒæ°2,...]);
åŒæ°ãªã:
const ãªããžã§ã¯ã = new ã¯ã©ã¹å;
ã¯ã©ã¹ã§å®çŸ©ããã¡ãœããã¯ã€ã³ã¹ã¿ã³ã¹ããå®è¡ã§ããã
class TestCls {
method(arg) {
console.log(`åŒæ°:[${arg}]ã§ã¡ãœãããå®è¡`);
}
}
const test = new TestCls;
test.method("ãã¹ã");
> åŒæ°[ãã¹ã]ã§ã¡ãœãããå®è¡
- éçã¡ãœãããšéçããããã£
- ã²ãã¿ãŒãšã»ãã¿ãŒ
- ã¯ã©ã¹ã®ç¶æ¿
- çæå ã¯ã©ã¹ã®ç¢ºèª
- hasOwnPropertyã¡ãœãããšinæŒç®å
ã€ã³ã¹ã¿ã³ã¹åãäžèŠã§ããã®ãŸãŸã¯ã©ã¹ããå©çšã§ããéçããããã£ãéçã¡ãœããã éçããããã£ã¯ã¹ã¿ãã£ãã¯ããããã£ãéçã¡ãœããã¯ã¹ã¿ãã£ãã¯ã¡ãœãããšåŒã¶ã
[æ§æ]éçã¡ãœãããéçããããã£ã®å®çŸ©æ¹æ³
class ã¯ã©ã¹å {
static ããããã£å = å€;
static ã¡ãœããå() {...}
}
[æ§æ]éçã¡ãœãããéçããããã£ã®å©çšæ¹æ³
ã¯ã©ã¹å.ããããã£å;
ã¯ã©ã¹å.ã¡ãœããå();
(䜿çšäŸ)
class Human {
static TYPE = "æ®éã®äºº";
static staticMove() {
console.log(Human.TYPE + "ã¯æ©ããŠç§»åããŸãã");
}
constructor(name) {
this.name = name;
}
move() {
console.log(this.name + "ã¯æ©ããŠç§»åããŸãã");
}
}
const taro = new Human("倪é");
Human.staticMove();
> æ®éã®äººã¯æ©ããŠç§»åããŸãã
console.log(Human.TYPE);
> æ®éã®äºº
taro.move();
> 倪éã¯æ©ããŠç§»åããŸãã
- éçã¡ãœããããä»ã®éçã¡ãœãããéçã¡ãœããã¯ãã¯ã©ã¹åã䜿ã£ãŠåç §ã§ããã
- éçã¡ãœããããã€ã³ã¹ã¿ã³ã¹ã®ã¡ãœãããããããã£ã¯ãåç §ã§ããŸããã
- ã€ã³ã¹ã¿ã³ã¹ã¡ãœããããä»ã®éçã¡ãœãããéçããããã£ã¯ãã¯ã©ã¹åãŸãã¯ãthis.constructorã䜿ã£ãŠåç §ã§ããã
- ã€ã³ã¹ã¿ã³ã¹ã¡ãœããããä»ã®ã€ã³ã¹ã¿ã³ã¹ã®ã¡ãœãããããããã£ã¯ãthisãéããŠåç §ã§ããã
ã¯ã©ã¹ãèšè¿°ããŠãããšãããããã£ã®å€ãååŸã»å€æŽãããšãã«ç¹å®ã®åŠçãããããŠå®è¡ãããå Žåãããã
ãã®æã«äœ¿ãã®ãã**ã²ãã¿ãŒ(ã²ãã¿ãŒã¡ãœãã)ãã»ãã¿ãŒ(ã»ãã¿ãŒã¡ãœãã)**ã§ãã
[æ§æ] ã²ãã¿ãŒãšã»ãã¿ãŒã®å®çŸ©æ¹æ³
class ã¯ã©ã¹å {
get ã²ããããããã£() {
return ã²ããããããã£ãåç
§ããéã«ååŸãããå€;
}
set ã»ããããããã£(èšå®ãããå€) {
ããããã£ã«å€ãèšå®ãããšãã«å®è¡ãããåŠç;
}
}
[æ§æ] ã²ãã¿ãŒãšã»ãã¿ãŒã®äœ¿çšæ¹æ³
const obj = new ã¯ã©ã¹å
console.log(obj.ã²ããããããã£);
obj.ã»ãããããã㣠= å€;
[䜿çšäŸ]
class Person {
constructor(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
get fullname() {
return this.lastname + this.firstname;
}
set age( value ) {
this._age = Number(value);
}
get age() {
return this._age;
}
}
const taro = new Person("倪é", "ç¬ç¿");
console.log(taro.fullname());
> ç¬ç¿å€ªé
taro.age = "18";
console.log(typeof taro.age);
> number
console.log(taro);
> {firstname: "倪é", lastname: "ç¬ç¿", _age: 18}
ã¯ã©ã¹ã®ç¶æ¿ãšã¯ãæ¢åã®ã¯ã©ã¹ãç¶æ¿ãã(åŒãç¶ã)ããšã§ãæ¢åã®ã¯ã©ã¹ã®æ©èœãå©çšããŠãå°ãæ©èœã®ç°ãªãã¯ã©ã¹ãæ°ãã«çæããèšæ³ã§ãã
ããã«ãããæ¢åã®ã¯ã©ã¹ã®ã³ãŒããåå©çšã§ããåé·ãªèšè¿°ãæžããããšãã§ããã
class Parent {
constructor(value) {
this.parentProp = value;
}
parentMethod() {
console.log("芪ã¯ã©ã¹ã®ã¡ãœãã");
}
}
class Child extends Parent {
constructor(parentProp, childProp) {
super(parentProp);
this.childProp = childProp;
}
childMethod() {
console.log(`åãã芪ã«ã¢ã¯ã»ã¹[${this.parentProp}]`);
}
}
const childObject = new Child("芪", "å");
console.log(childObject);
> {parentProp: "芪", childProp: "å"}
childObject.parentMethod();
> 芪ã¯ã©ã¹ã®ã¡ãœãã
childObject.childMethod();
> åãã芪ã«ã¢ã¯ã»ã¹[芪]
- extends ã§ã¯ã©ã¹ã®ç¶æ¿
- superããŒã¯ãŒãã§èŠªã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãå®è¡
ã€ã³ã¹ã¿ã³ã¹ã®çæå ã¯ã©ã¹ã®ç¢ºèªããã«ã¯ãinstanceofæŒç®åã䜿ãã
[æ§æ] instanceofã®èšæ³
let result = ã€ã³ã¹ã¿ã³ã¹ instanceof ã¯ã©ã¹å;
ã€ã³ã¹ã¿ã³ã¹ã®çæå
ã¯ã©ã¹åãinstanceofã®å³ãªãã©ã³ãã®ã¯ã©ã¹åãšäžèŽããå Žåã«ã¯trueãè¿ããäžèŽããªãå Žåã«ã¯falseãè¿ããŸãã
ãã®ã¯ã©ã¹åã®æ¯èŒå¯Ÿè±¡ã«ã¯ãç¶æ¿å
ã®ã¯ã©ã¹ãå«ãŸãã
class Parent { }
class Child extends Parent { }
const obj = new Child;
console.log(obj instanceof Child);
> true
console.log(obj instanceof Parent);
> true
hasOwnPropertyã¯èªèº«ã®ãªããžã§ã¯ãã«ããããã£ãååšãããã確èªããã¡ãœããã§ãããåããããªæ©èœãæã€inæŒç®åãããã
[æ§æ] hasOwnPropertyã®èšæ³
let çåœå€ = ãªããžã§ã¯ã.hashOwnProperty("ããããã£å");
[æ§æ] in æŒç®åã®èšæ³
let çåœå€ = "ããããã£å" in ãªããžã§ã¯ã;
hasOwnPropertyã®çµæãtrueã«ãªãã®ã¯ãããããã£åãèªèº«ã®ãªããžã§ã¯ãã®ããããã£ãšããŠååšããå Žåã§ãã
ç¶æ¿ããã¯ã©ã¹ã®ããããã£ãå«ãŸããããã ããã¡ãœããåãšäžèŽããŠãtrueã«ã¯ãªããªãã®ã§æ³šæã
inæŒç®åã®çµæãtrueã«ãªãã®ã¯ããªããžã§ã¯ããä¿æããããããã£ãŸãã¯ã¡ãœãããšäžèŽããå Žåã§ãã
ç¶æ¿ããã¯ã©ã¹ã®ããããã£ãã¡ãœãããå«ãŸããã
ES2022ã®ããŒãžã§ã³ãããã¯ã©ã¹ã®æ©èœãæ¡åŒµãããŠããããã©ãŠã¶ã«ãã£ãŠã¯ããŸã æ©èœãå®è£
ãããŠããªãå¯èœæ§ãããããã
å®éã®ç°å¢ã§äœ¿ãããã«ã¯Bableãwebpackãšãã£ããå€ããã©ãŠã¶ã§ãåãã³ãŒãã«å€æããŠããããœãããŠã§ã¢ãå¿
èŠã«ãªãå ŽåããããŸãã
ES6(ES2015)ã®ã¯ã©ã¹èšæ³ã§ã¯ãããããã£ãèšå®ããå Žåã«ã¯ãå¿
ãã³ã³ã¹ãã©ã¯ã¿å
ã§èšè¿°ããå¿
èŠãããã
ES2022ã®ã¯ã©ã¹ã®èšæ³ã§ã¯ãã¯ã©ã¹ã®ãããã¬ãã«ã§ããããã£ã宣èšã§ããããã«ãªã£ãã
ããã«ãããããããã£ã«å€ãèšå®ããã ãã§ããã°ãã³ã³ã¹ãã©ã¯ã¿ãçç¥ã§ããã
(ES2015ã®å Žå):
class ES2015 {
constructor() {
this.prop = 0;
}
}
// ES2022ã®ã¯ã©ã¹ã§æžããå Žå
class ES2022 {
prop = 0; // ã¯ã©ã¹ã®ãããã¬ãã«ã§ããããã£ãèšå®å¯èœ
}
ãªããã³ã³ã¹ãã©ã¯ã¿ã®åŒæ°ãåãåã£ãŠããããã£ã«èšå®ãããå Žåã¯ã
ES2015ã®ã¯ã©ã¹ãšåæ§ã«ãã³ã³ã¹ãã©ã¯ã¿å
ã§åæå€ãèšå®ããå¿
èŠãããã
class ES2022 {
prop1 = 0; // ã³ã³ã¹ãã©ã¯ã¿ã®åŒæ°ã代å
¥ããªãå Žå
constructor(arg) {
this.prop2 = arg; // ã³ã³ã¹ãã©ã¯ã¿ã®åŒæ°ã代å
¥ããå Žåãconstructorå
ã§è¡ãã
}
}
ES2022ã®ã¯ã©ã¹èšæ³ã§ã¯èªã¯ã©ã¹å
ããã®ã¿ã¢ã¯ã»ã¹å¯èœãªããããã£ãã¡ãœãããå®çŸ©ã§ããããã«ãªã£ãã
ES2022ã®ã¯ã©ã¹ã§ã¯ãããªãã¯ãšãã©ã€ããŒãã®2çš®é¡ã®ã¢ã¯ã»ã¹æš©ã䜿ãåããããšãã§ããã
ãããªãã¯ã¯ãã¯ã©ã¹ã®å€ããã§ãããããã£ãã¡ãœããã«ã¢ã¯ã»ã¹å¯èœãªç¶æ ã«ããã
ãã©ã€ããŒãã¯ãèªã¯ã©ã¹å
ããã®ã¿ã¢ã¯ã»ã¹å¯èœãªç¶æ
ãè¡šãããã®ããããã£ãã¡ãœããã®å®çŸ©æ¹æ³ã
ES2022ããè¿œå ãããä»æ§ã(ãã©ã€ããŒãããããã£ããã©ã€ããŒãã¡ãœãããšåŒã¶ã)
ãã©ã€ããŒãããããã£ããã©ã€ããŒãã¡ãœãããå®çŸ©ããå Žåã«ã¯ãå
é ã« # ãã€ããã
class Counter {
#count = 0; // ãã©ã€ããŒãããããã£
#print() { // ãã©ã€ããŒãã¡ãœãã
console.log(this.count);
}
increment() { // ãããªãã¯ã¡ãœãã
this.#count++;
this.#print(); // ãã©ã€ããŒãã¡ãœãããå®è¡
}
}
const counter = new Counter();
counter.increment();
> 1
JavaScriptã¯ãããã¿ã€ãããŒã¹èšèªãšåŒã°ããèšèªä»æ§ã®æ ¹åºã«ã¯é¢æ°ãšå¯æ¥ã«é¢ãã**ãããã¿ã€ã(prototype)**ãšåŒã°ããä»çµã¿ãããã
ES6ããä»æ§è¿œå ãããã¯ã©ã¹ããè£åŽã§åããŠããä»çµã¿ã¯ãããã¿ã€ãã§ãããããèªåã®æãéãã«ã³ãŒããåäœãããã«ã¯ã
ãããã¿ã€ãã®ä»çµã¿ãç解ããå¿
èŠãããã
ES5ã®ããŒãžã§ã³ãŸã§ã¯ãJavaScriptã®ãªããžã§ã¯ãã¯ã³ã³ã¹ãã©ã¯ã¿é¢æ°ãšnewæŒç®åã䜿ã£ãŠçæããŠããŸããã
ã³ã³ã¹ãã©ã¯ã¿é¢æ°ãšã¯ãclasså
ã§äœ¿çšããã³ã³ã¹ãã©ã¯ã¿(constructor)ãšåæ§ã®åããããé¢æ°ã§ãã
次ã®ããã«å®çŸ©ããã
[æ§æ] ã³ã³ã¹ãã©ã¯ã¿é¢æ°ã®å®çŸ©
function FunctionName([åŒæ°1, åŒæ°2,....]) {
this.ããããã£å = å€;
}
ã» ã³ã³ã¹ãã©ã¯ã¿é¢æ°ã®é¢æ°å(FunctionName)ã«ã¯ãäžè¬çãªé¢æ°ãšåºå¥ãããããã¹ã«ã«ã±ãŒã¹ã䜿ãã
ã» ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿å
ã®thisãšåæ§ã«ãçæããããªããžã§ã¯ãã®ã€ã³ã¹ã¿ã³ã¹ãåç
§ããããŸããã€ã³ã¹ã¿ã³ã¹åãè¡ããšããã
ã¯ã©ã¹ãšåæ§ã«newæŒç®åã䜿çšããã
JavaScriptèšèªã®äžå¿çãªä»æ§ã®1ã€ã§ãããããã¿ã€ã(prototype)ã«ã€ããŠã次ã®4ã€ã®ç¹åŸŽãããã
é¢æ°ããªããžã§ã¯ãã®äžçš®ãªã®ã§ãé¢æ°ã«ãããããã£ãä¿æã§ããã
ãããã¿ã€ã(prototype)ã¯ãã€ã³ã¹ã¿ã³ã¹åã«é¢ä¿ããç¹å¥ãªããããã£ãšããŠé¢æ°ãªããžã§ã¯ãã«ä¿æãããã
function Test() {}
// é¢æ°ã¯ãªããžã§ã¯ãã®äžçš®
Test.prop = "å€"; // Testé¢æ°ã¯ãªããžã§ã¯ããªã®ã§ãããããã£ã«å€ãèšå®ã§ãã
console.log(Test.prop);
> å€
// prototypeããããã£ã®ååšç¢ºèª
console.log("prototype" in Test);
> true // é¢æ°ãå®çŸ©ãããšprototypeããããã£ãèªåçã«èšå®ãããã
// prototypeã«ã¯ãªããžã§ã¯ããæ ŒçŽãããŠãã
console.log(typeof Test.prototype);
> object
prototypeãªããžã§ã¯ã(prototypeããããã£ã«èšå®ãããŠãããªããžã§ã¯ã)ã«ç»é²ãããé¢æ°ã¯ã
ã€ã³ã¹ã¿ã³ã¹ããå®è¡å¯èœãªã¡ãœããã«ãªãã
function Person(name) {
this.name = name;
}
Person.prototype.hello = function() { // Personã³ã³ã¹ãã©ã¯ã¿ã®prototypeãªããžã§ã¯ãã®helloããããã£ã«ç¡åé¢æ°ãç»é²
console.log(`ããã«ã¡ã¯ã${this.name}`);
}
const taro = new Person("ç¬ç¿å€ªé");
taro.hello();
> ããã«ã¡ã¯ãç¬ç¿å€ªé
const hanako = new Person("ç¬ç¿è±å");
hanako.hello();
> ããã«ã¡ã¯ãç¬ç¿è±å
newæŒç®åã«ãã£ãŠãã³ã³ã¹ãã©ã¯ã¿ããã€ã³ã¹ã¿ã³ã¹ãäœæãããšããã³ã³ã¹ãã©ã¯ã¿é¢æ°ã®prototypeããããã£ã«
æ ŒçŽãããŠãããªããžã§ã¯ããžã®åç
§ããã€ã³ã¹ã¿ã³ã¹ã®**proto**ãšããç¹å¥ãªããããã£ã«ã³ããŒãããã
function Test() { }
Test.prototype.hello = function() { console.log("ããã«ã¡ã¯") };
const instance = new Test;
console.log(instance.__proto__ === Test.prototype); // __proto__ãšprototypeã¯åããªããžã§ã¯ã
> true
instance.__proto__.hello();
> ããã«ã¡ã¯
__proto__ã¯ãèšè¿°ãçç¥ããããšãã§ããããã®ãããäžè¬çã«ã¯ã€ã³ã¹ã¿ã³ã¹ã®ã¡ãœãããå®è¡æã«ã¯ã__proto__ã¯èšè¿°ããŸããã
function Test() { }
Test.prototype.hello = function() { console.log("ããã«ã¡ã¯") };
const instance = new Test;
console.log(instance.__proto__ === Test.prototype); // __proto__ãšprototypeã¯åããªããžã§ã¯ã
> true
instance.hello();
> ããã«ã¡ã¯
ã¯ã©ã¹èšæ³ã䜿ã£ãå Žåã§ããè£åŽã§åäœããä»çµã¿ã¯ãããã¿ã€ãããŒã¹ã
ãã®ãããã¯ã©ã¹ã§å®çŸ©ããã¡ãœããã®å Žåãåæ§ã«ã__proto__ãéããŠã¡ãœãããå®è¡ãããã
class Test {
hello() {
console.log("ããã«ã¡ã¯");
}
}
const instance = new Test;
Test.prototype.hello();
> ããã«ã¡ã¯
instance.__proto__.hello();
> ããã«ã¡ã¯
instance.hello();
> ããã«ã¡ã¯
prototypeãš__proto__ã¯ãåããªããžã§ã¯ããžã®åç
§ãä¿æããŠãããããprototypeã®ã¡ãœããã«å¯ŸããŠè¡ã£ãå€æŽã¯ã
ãã¹ãŠã®ã€ã³ã¹ã¿ã³ã¹ã«åæ ãããã
class Test {
hello() {
console.log("ããã«ã¡ã¯");
}
}
const instance = new Test;
Test.prototype.hello = function() {
console.log("Hello");
}
instance.hello();
> Hello
ãããã¿ã€ããå€éå±€ã«ãªã£ãŠãããšãã®æåãç解ãããã€ã³ãã¯ã次ã®3ã€ã§ãã
â ã»ãŒãã¹ãŠã®ãªããžã§ã¯ãã¯__proto__ãšããç¹æ®ãªããããã£ãä¿æããã(ãã ãäŸå€çã«ä¿æããªãå Žåããã)
â¡ ãªããžã§ã¯ãã®ããããã£ãåç
§ãããšãããªããžã§ã¯ãå
ã«ããããã£ãèŠã€ãããªããã°ãæé»çã«__proto__ãªããžã§ã¯ãå
ã®
ããããã£ãã¡ãœãããæ¢ãã«è¡ãã(ãã®ããã__proto__ãçç¥ããŠ__proto__ã®ããããã£ãã¡ãœããã«ã¢ã¯ã»ã¹ã§ãã)
⢠__proto__ã«äžèŽããããããã£ãèŠã€ãããªãã£ãå Žåã¯ãããã«__proto__ã«äžèŽããããããã£ãæ¢ãè¡ãããšã«ãªãã
ãã®ããã次ã®ã³ãŒãã®ããã«__proto__ãè€æ°éå±€ã«é£ãªã£ãŠããç¶æ
ã®å Žåã§ããobj.hello()ãšèšè¿°ããŠã¡ãœãããå®è¡ããã°ã
obj.proto.proto.hello()ãå®è¡ãããããšã«ãªãã
const obj = {
__proto__: {
__proto__: {
hello() {
console.log("ããã«ã¡ã¯");
}
},
},
};
obj.hello();
> ããã«ã¡ã¯
JavaScriptãšã³ãžã³ã¯ã¡ãœãããèŠã€ãããŸã§ã__proto__ãã©ãã©ãããã®ãŒã£ãŠãããæåã«èŠã€ãã£ãã¡ãœãããå®è¡ããã
ãã®ããã«__proto__ãé£ãªã£ãŠããç¶æ
ããããã¿ã€ããã§ãŒã³ãšåŒã¶
ãªããäžèšããªããžã§ã¯ãã«å¯ŸããŠçŽæ¥__proto__ã«èšå®ããŠãããããã®æ¹æ³ã¯æšå¥šãããŠããªãããããã¿ã€ããã§ãŒã³ãäœæããã«ã¯ã
ã¯ã©ã¹ã®ç¶æ¿ãè¡ããããããã¯Object.createã¡ãœãããªã©ã䜿ã£ãŠã__proto__ãå«ããªããžã§ã¯ããäœæããã
[æ§æ] Object.createã®äœ¿çšæ¹æ³
const resObj = Object.create(protoObj)
resObj: Object.createã®åŒæ°ã«æå®ãããªããžã§ã¯ã(protoObj)ãžã®åç
§ã__proto__ã«æ ŒçŽãã空ã®ãªããžã§ã¯ããäœæãããã
ä»®ã«protoObjã{hello: function() {}}ã®ãããªãªããžã§ã¯ãã®å ŽåãObject.create(protoObj)ãå®è¡ãããšãresObjã¯æ¬¡ã®ãããªãªããžã§ã¯ããçæãã
{
__proto__: {hello: function() {}}
}
protoObj:__proto__ã®åç
§å
ãšããŠèšå®ããããªããžã§ã¯ããæž¡ãã
(䜿çšäŸ)
const fruit1 = {
apple: function() {
console.log("ãªã³ãŽ");
},
};
const fruit2 = Object.create(fruit1);
fruit2.banana = function() {
console.log("ããã");
};
const fruit3 = Object.create(fruit2);
fruit3.melon = function() {
console.log("ã¡ãã³");
};
fruit3.apple();
> ãªã³ãŽ
fruit3.banana();
> ããã
fruit3.melon();
> ã¡ãã³
ä»ã®ãªããžã§ã¯ãã__proto__ã«èšå®ããããšã§ã__proto__ãéããŠä»ã®ãªããžã§ã¯ãã®æ©èœãå©çšã§ãããªããžã§ã¯ããäœæããŸããã
ãã®ããã«ãä»ã®ãªããžã§ã¯ãã®æ©èœãåŒãç¶ãããšãç¶æ¿ãšåŒã³ãä»ã®ã³ã³ã¹ãã©ã¯ã¿ã®prototypeãç¶æ¿ããããšããããã¿ã€ãç¶æ¿ãšåŒã¶ã
// 芪ã®ã³ã³ã¹ãã©ã¯ã¿ã宣èš
function Parent() { }
// åã®ã³ã³ã¹ãã©ã¯ã¿ã宣èš
function Child() { }
// 芪ã®ãããã¿ã€ãã«ã¡ãœãããè¿œå
Parent.prototype.parentMethod = function() {
console.log("芪ã®ã¡ãœãã");
};
// 芪ã®ãããã¿ã€ããç¶æ¿
Child.prototype = Object.create(Parent.prototype);
// åã®ãããã¿ã€ãã«ã¡ãœãããè¿œå
Child.prototype.childMethod = function() {
console.log("ãã®ã¡ãœãã");
}
// ã€ã³ã¹ã¿ã³ã¹å
const childObj = new Child;
childObj.parentMethod();
> 芪ã®ã¡ãœãã
childObj.childMethod();
> åã®ã¡ãœãã
èªèº«ã®ãªããžã§ã¯ãã®ããããã£ãšããŠååšãããã©ããã確èªããŸãã
ãããã¿ã€ããã§ãŒã³ãŸã§å«ããŠããããã£ãååšãããã確èªããŸãã