ãã§ã¼ã³
éè¦æ§: 2
ä¸ä¸ã«ç§»åã§ãã ladder ãªãã¸ã§ã¯ããããã¾ãã:
let ladder = {
step: 0,
up() {
this.step++;
},
down() {
this.step--;
},
showStep: function() { // ç¾å¨ã®æ®µã表示ãã¾ã
alert( this.step );
}
};
ãã¦ãé çªã«ããã¤ãã®å¼ã³åºããããå ´åããã®ããã«ã§ãã¾ã:
ladder.up();
ladder.up();
ladder.down();
ladder.showStep(); // 1
up 㨠down ã®ã³ã¼ããä¿®æ£ãã¦ãé£éå¯è½ãªå¼ã³åºããã§ããããã«ãã¦ãã ããã:
ladder.up().up().down().showStep(); // 1
ãã®ãããªã¢ããã¼ãã¯JavaScriptã©ã¤ãã©ãªã®ä¸ã§åºã使ããã¦ãã¾ãã
解決çã¯ãã¹ã¦ã®å¼ã³åºãã§ãªãã¸ã§ã¯ãèªèº«ãè¿ããã¨ã§ãã
let ladder = {
step: 0,
up() {
this.step++;
return this;
},
down() {
this.step--;
return this;
},
showStep() {
alert( this.step );
return this;
}
}
ladder.up().up().down().up().down().showStep(); // 1
ã¾ãã1è¡æ¯ã«1ã¤ã®å¼ã³åºãã§æ¸ããã¨ãã§ãã¾ããé·ãé£éã®å ´åã¯ããèªã¿ãããã§ãã:
ladder
.up()
.up()
.down()
.up()
.down()
.showStep(); // 1
let ladder = {
step: 0,
up: function() {
this.step++;
return this;
},
down: function() {
this.step--;
return this;
},
showStep: function() {
alert(this.step);
}
};
ãµã³ãããã¯ã¹ã§ãã¹ãã¨ä¸ç·ã«è§£çãéã
