最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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 :

发布评论

评论列表(0)

  1. 暂无评论