javascript - What is a difference between inherits and extends of classes? - Stack Overflow

I was looking for the asnwer long time and i get it a bit plicated. What is a difference between inheri

I was looking for the asnwer long time and i get it a bit plicated. What is a difference between inherits and extends of classes ?

My question was born after reading this ebook and i was using extends syntax so it maked me wonder.

Extends Classes

class A {
    a = 2;

    constructor(x) {
        this.a = x;
    }
}

class B extends A {
}

Class Inheritance

class A {
    a = 4;

    A(x) {
        a = x;
    }

    drive() {
        output( "A" )
    }
}
class B inherits A {
    drive() {
        inherited:drive()
        output( "B" )
    }
}

Can i use constructor when inherits classes ? or name constructor when extend classes ?

What is the differenc when using super or inherited ?

Can i use inherited syntax when extending class ? I read that super is a direct way for the constructor of a child class to reference the constructor of its parent class.

I was looking for the asnwer long time and i get it a bit plicated. What is a difference between inherits and extends of classes ?

My question was born after reading this ebook and i was using extends syntax so it maked me wonder.

Extends Classes

class A {
    a = 2;

    constructor(x) {
        this.a = x;
    }
}

class B extends A {
}

Class Inheritance

class A {
    a = 4;

    A(x) {
        a = x;
    }

    drive() {
        output( "A" )
    }
}
class B inherits A {
    drive() {
        inherited:drive()
        output( "B" )
    }
}

Can i use constructor when inherits classes ? or name constructor when extend classes ?

What is the differenc when using super or inherited ?

Can i use inherited syntax when extending class ? I read that super is a direct way for the constructor of a child class to reference the constructor of its parent class.

Share Improve this question edited Feb 11, 2016 at 11:15 vardius asked Feb 11, 2016 at 11:01 vardiusvardius 6,5769 gold badges59 silver badges103 bronze badges 3
  • 2 As far as I'm aware inherits is not a keyword in ES6...!? – deceze Commented Feb 11, 2016 at 11:08
  • 1 There is no mention of inherits in the spec – Davin Tryon Commented Feb 11, 2016 at 11:09
  • 2 Inheritance (noun, not keyword) is a concept. extends (ES6 keyword) is an implementation of that concept. When one class extends another, it inherits its properties... – deceze Commented Feb 11, 2016 at 11:11
Add a ment  | 

2 Answers 2

Reset to default 7

inherits is not a keyword in ES6. In that place of a class declaration, only extends is valid, you've got a syntax error.

And neither are a = 4; in a class body nor inherited:drive(). The book section you found this in even explicitly states "Consider this loose pseudo-code (invented syntax) for inherited classes".

Inheriting refers to the relationship between a derived class (the child) and the base class (the parent). The derived class can use certain methods and fields within the base class according to accessibility levels

Extending is interchangeable with Inheriting and usually is used in java (since the syntax for inheritance in java is the keyword extends. In C#, it is colon :

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744615956a4584054.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信