반응형
prototype 과 상속 (pseudo-classical style)
var Horse = function(name) {
this.name = name;
};
Horse.prototype.goSomewhere = function(destination) {
return this.name + " is galloping to " + destination + "!";
};
var FlyingHorse = function(name, color) {
Horse.call(this, name);
this.color = color;
this.oldGoSomewhere = Horse.prototype.goSomewhere.bind(this);
};
FlyingHorse.prototype = Object.create(Horse.prototype);
FlyingHorse.prototype.constructor = FlyingHorse;
FlyingHorse.prototype.goSomewhere = function(destination, milesToDestination) {
if (milesToDestination < 10) {
return this.oldGoSomewhere(destination);
}
if (milesToDestination >= 10) {
return this.name + " is flying to " + destination + "!";
}
};
어제 시험보다가 제대로 멘붕 왔던 구간 ^^;; 당연히 기억할 줄 알아서 대충 보고 넘어 갔는데, 나의 기억을 너무 맹신한듯..? 덕분에 한 일주일은 기억이 유지 될 듯.. 하!
Horse.call(this, name) : super(name) 과 같은 역할.
FlyingHorse.prototype = Object.create(Horse.prototype)
: FlyingHorse 가 Horse의 prototype을 상속할 수 있게 하는 부분
FlyingHorse가 Horse의 goSomewhere과 다른 기능을 하게 하려면,
: FlyingHorse.prototype = Object.create(Horse.prototype) 다음 줄에 썼어야 함 (그냥 위치랑 상관 없는줄..)
printArray
var printArray = function(arr) {
let result;
result = result || [];
function recurse(array) {
for (let el of array) {
if (!Array.isArray(el)) {
el !== undefined ? result.push(el) : result;
}
if (Array.isArray(el)) {
recurse(el);
}
}
}
recurse(arr);
result = result.join("");
console.log(result);
}
test 케이스가 무슨 의미인지를 몰라 멘붕이 왔던 문제.
console.log를 조작하는 거였는데, 몰랐음;;;하;;
var newArray = "";
var oldLog = console.log;
console.log = function(item) {
newArray += item;
};
printArray([1, 2, , [3, 4], 5]);
console.log = oldLog;
newArray.should.eql("12345");
반응형
'2. 우당탕탕 개발자 > 2-1. 공부기록' 카테고리의 다른 글
14Jan2020 TIL (0) | 2020.01.14 |
---|---|
13Jan2020 TIL (0) | 2020.01.13 |
10Jan2020 TIL (0) | 2020.01.11 |
09Jan2020 TIL (0) | 2020.01.10 |
06Jan2020 TIL (0) | 2020.01.07 |
댓글