how i can to realize inheritance chain?










2















Have some problem with inheritance. How to implement type inheritance from Some Pencil to Another Pencil? Need: pen3.type // common



 class Pencil 
constructor(color)
this.color = color;

intro()
console.log(`this is $this.color pencil`);

;

class SomePencil extends Pencil
constructor(color, type)
super(color);
this.type = type;

;
class AnotherPencil extends SomePencil
constructor(color,type)
super(color, type);

;

let pen1 = new Pencil();
let pen2 = new SomePencil("red", "common");
let pen3 = new AnotherPencil("green");

console.log("type" in pen3); // true









share|improve this question

















  • 1





    I'm not clear what you need. Do you want all instances of AnotherPencil to have type="common"?

    – Nicholas Tower
    Nov 13 '18 at 16:17












  • pen3.type !== undefined

    – Keith
    Nov 13 '18 at 16:23











  • pen3 has a property type because SomePencil has a type but that type is undefined because it hasn't been set with a value when the constructor is called. This is the way it's supposed to work. What do you want the result to be?

    – Mark Meyer
    Nov 13 '18 at 16:27












  • Remember to always favor Composition over inheritance

    – David Lemon
    Nov 13 '18 at 16:27












  • Fix it by calling const pen3 = new AnotherPencil("green", "common");

    – Bergi
    Nov 13 '18 at 17:26















2















Have some problem with inheritance. How to implement type inheritance from Some Pencil to Another Pencil? Need: pen3.type // common



 class Pencil 
constructor(color)
this.color = color;

intro()
console.log(`this is $this.color pencil`);

;

class SomePencil extends Pencil
constructor(color, type)
super(color);
this.type = type;

;
class AnotherPencil extends SomePencil
constructor(color,type)
super(color, type);

;

let pen1 = new Pencil();
let pen2 = new SomePencil("red", "common");
let pen3 = new AnotherPencil("green");

console.log("type" in pen3); // true









share|improve this question

















  • 1





    I'm not clear what you need. Do you want all instances of AnotherPencil to have type="common"?

    – Nicholas Tower
    Nov 13 '18 at 16:17












  • pen3.type !== undefined

    – Keith
    Nov 13 '18 at 16:23











  • pen3 has a property type because SomePencil has a type but that type is undefined because it hasn't been set with a value when the constructor is called. This is the way it's supposed to work. What do you want the result to be?

    – Mark Meyer
    Nov 13 '18 at 16:27












  • Remember to always favor Composition over inheritance

    – David Lemon
    Nov 13 '18 at 16:27












  • Fix it by calling const pen3 = new AnotherPencil("green", "common");

    – Bergi
    Nov 13 '18 at 17:26













2












2








2


1






Have some problem with inheritance. How to implement type inheritance from Some Pencil to Another Pencil? Need: pen3.type // common



 class Pencil 
constructor(color)
this.color = color;

intro()
console.log(`this is $this.color pencil`);

;

class SomePencil extends Pencil
constructor(color, type)
super(color);
this.type = type;

;
class AnotherPencil extends SomePencil
constructor(color,type)
super(color, type);

;

let pen1 = new Pencil();
let pen2 = new SomePencil("red", "common");
let pen3 = new AnotherPencil("green");

console.log("type" in pen3); // true









share|improve this question














Have some problem with inheritance. How to implement type inheritance from Some Pencil to Another Pencil? Need: pen3.type // common



 class Pencil 
constructor(color)
this.color = color;

intro()
console.log(`this is $this.color pencil`);

;

class SomePencil extends Pencil
constructor(color, type)
super(color);
this.type = type;

;
class AnotherPencil extends SomePencil
constructor(color,type)
super(color, type);

;

let pen1 = new Pencil();
let pen2 = new SomePencil("red", "common");
let pen3 = new AnotherPencil("green");

console.log("type" in pen3); // true






javascript oop






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 16:09









disco_dinosaurdisco_dinosaur

164




164







  • 1





    I'm not clear what you need. Do you want all instances of AnotherPencil to have type="common"?

    – Nicholas Tower
    Nov 13 '18 at 16:17












  • pen3.type !== undefined

    – Keith
    Nov 13 '18 at 16:23











  • pen3 has a property type because SomePencil has a type but that type is undefined because it hasn't been set with a value when the constructor is called. This is the way it's supposed to work. What do you want the result to be?

    – Mark Meyer
    Nov 13 '18 at 16:27












  • Remember to always favor Composition over inheritance

    – David Lemon
    Nov 13 '18 at 16:27












  • Fix it by calling const pen3 = new AnotherPencil("green", "common");

    – Bergi
    Nov 13 '18 at 17:26












  • 1





    I'm not clear what you need. Do you want all instances of AnotherPencil to have type="common"?

    – Nicholas Tower
    Nov 13 '18 at 16:17












  • pen3.type !== undefined

    – Keith
    Nov 13 '18 at 16:23











  • pen3 has a property type because SomePencil has a type but that type is undefined because it hasn't been set with a value when the constructor is called. This is the way it's supposed to work. What do you want the result to be?

    – Mark Meyer
    Nov 13 '18 at 16:27












  • Remember to always favor Composition over inheritance

    – David Lemon
    Nov 13 '18 at 16:27












  • Fix it by calling const pen3 = new AnotherPencil("green", "common");

    – Bergi
    Nov 13 '18 at 17:26







1




1





I'm not clear what you need. Do you want all instances of AnotherPencil to have type="common"?

– Nicholas Tower
Nov 13 '18 at 16:17






I'm not clear what you need. Do you want all instances of AnotherPencil to have type="common"?

– Nicholas Tower
Nov 13 '18 at 16:17














pen3.type !== undefined

– Keith
Nov 13 '18 at 16:23





pen3.type !== undefined

– Keith
Nov 13 '18 at 16:23













pen3 has a property type because SomePencil has a type but that type is undefined because it hasn't been set with a value when the constructor is called. This is the way it's supposed to work. What do you want the result to be?

– Mark Meyer
Nov 13 '18 at 16:27






pen3 has a property type because SomePencil has a type but that type is undefined because it hasn't been set with a value when the constructor is called. This is the way it's supposed to work. What do you want the result to be?

– Mark Meyer
Nov 13 '18 at 16:27














Remember to always favor Composition over inheritance

– David Lemon
Nov 13 '18 at 16:27






Remember to always favor Composition over inheritance

– David Lemon
Nov 13 '18 at 16:27














Fix it by calling const pen3 = new AnotherPencil("green", "common");

– Bergi
Nov 13 '18 at 17:26





Fix it by calling const pen3 = new AnotherPencil("green", "common");

– Bergi
Nov 13 '18 at 17:26












1 Answer
1






active

oldest

votes


















1














Inheritance will only inherit properties and not values unless they are set in the class definition itself. For example



class SomePencil extends Pencil 
constructor(color, type)
super(color);
this.type = type
;


new way to set defaults



class SomePencil extends Pencil 
constructor(color, type = 'common')
super(color);
this.type = type;

;


Refer jsfiddle






share|improve this answer























  • thank you! I must be more attentive

    – disco_dinosaur
    Nov 13 '18 at 18:54










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285056%2fhow-i-can-to-realize-inheritance-chain%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Inheritance will only inherit properties and not values unless they are set in the class definition itself. For example



class SomePencil extends Pencil 
constructor(color, type)
super(color);
this.type = type
;


new way to set defaults



class SomePencil extends Pencil 
constructor(color, type = 'common')
super(color);
this.type = type;

;


Refer jsfiddle






share|improve this answer























  • thank you! I must be more attentive

    – disco_dinosaur
    Nov 13 '18 at 18:54















1














Inheritance will only inherit properties and not values unless they are set in the class definition itself. For example



class SomePencil extends Pencil 
constructor(color, type)
super(color);
this.type = type
;


new way to set defaults



class SomePencil extends Pencil 
constructor(color, type = 'common')
super(color);
this.type = type;

;


Refer jsfiddle






share|improve this answer























  • thank you! I must be more attentive

    – disco_dinosaur
    Nov 13 '18 at 18:54













1












1








1







Inheritance will only inherit properties and not values unless they are set in the class definition itself. For example



class SomePencil extends Pencil 
constructor(color, type)
super(color);
this.type = type
;


new way to set defaults



class SomePencil extends Pencil 
constructor(color, type = 'common')
super(color);
this.type = type;

;


Refer jsfiddle






share|improve this answer













Inheritance will only inherit properties and not values unless they are set in the class definition itself. For example



class SomePencil extends Pencil 
constructor(color, type)
super(color);
this.type = type
;


new way to set defaults



class SomePencil extends Pencil 
constructor(color, type = 'common')
super(color);
this.type = type;

;


Refer jsfiddle







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 16:21









AbhidevAbhidev

4,40651525




4,40651525












  • thank you! I must be more attentive

    – disco_dinosaur
    Nov 13 '18 at 18:54

















  • thank you! I must be more attentive

    – disco_dinosaur
    Nov 13 '18 at 18:54
















thank you! I must be more attentive

– disco_dinosaur
Nov 13 '18 at 18:54





thank you! I must be more attentive

– disco_dinosaur
Nov 13 '18 at 18:54

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285056%2fhow-i-can-to-realize-inheritance-chain%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3