"JS10 Day 5: Inheritance"의 두 판 사이의 차이

(새 문서: ==개요== ;<nowiki>JS10 Day 5: Inheritance</nowiki> * https://www.hackerrank.com/challenges/js10-inheritance/problem <source lang='javascript'> </source> <source lang='javascript'>...)
 
잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
 
(다른 사용자 한 명의 중간 판 3개는 보이지 않습니다)
3번째 줄: 3번째 줄:
* https://www.hackerrank.com/challenges/js10-inheritance/problem
* https://www.hackerrank.com/challenges/js10-inheritance/problem


<source lang='javascript'>
{{JS10 헤더}}
</source>
{{JS10 4-5}}
<source lang='javascript'>
|}
</source>
<source lang='javascript'>
</source>


==같이 보기==
----
* [[해커랭크 10 Days of Javascript]]


[[분류: 10 Days of Javascript]]
<syntaxhighlight lang='javascript'>
class Rectangle {
    constructor(w, h) {
        this.w = w;
        this.h = h;
    }
}
</syntaxhighlight>
<syntaxhighlight lang='javascript'>
/*
*  Write code that adds an 'area' method to the Rectangle class' prototype
*/
Rectangle.prototype.area = function() {
    return this.w * this.h;
}
/*
* Create a Square class that inherits from Rectangle and implement its class constructor
*/
class Square extends Rectangle {
    constructor(w) {
        super(w,w);
    }
}
</syntaxhighlight>
<syntaxhighlight lang='javascript'>
if (JSON.stringify(Object.getOwnPropertyNames(Square.prototype)) === JSON.stringify([ 'constructor' ])) {
    const rec = new Rectangle(3, 4);
    const sqr = new Square(3);
   
    console.log(rec.area());
    console.log(sqr.area());
} else {
    console.log(-1);
    console.log(-1);
}
</syntaxhighlight>

2020년 11월 2일 (월) 02:52 기준 최신판

개요[ | ]

JS10 Day 5: Inheritance
해커랭크 10 Days of Javascript
# 문제 비고
4-5 Day e
12 JS10 Day 4: Create a Rectangle Object
13 JS10 Day 4: Count Objects
14 JS10 Day 4: Classes
15 JS10 Day 5: Inheritance
16 JS10 Day 5: Template Literals
17 JS10 Day 5: Arrow Functions

class Rectangle {
    constructor(w, h) {
        this.w = w;
        this.h = h;
    }
}
/*
 *  Write code that adds an 'area' method to the Rectangle class' prototype
 */
Rectangle.prototype.area = function() {
    return this.w * this.h;
}
/*
 * Create a Square class that inherits from Rectangle and implement its class constructor
 */
class Square extends Rectangle {
    constructor(w) {
        super(w,w);
    }
}
if (JSON.stringify(Object.getOwnPropertyNames(Square.prototype)) === JSON.stringify([ 'constructor' ])) {
    const rec = new Rectangle(3, 4);
    const sqr = new Square(3);
    
    console.log(rec.area());
    console.log(sqr.area());
} else {
    console.log(-1);
    console.log(-1);
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}