how i can to realize inheritance chain?
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
|
show 1 more comment
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
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 propertytype
becauseSomePencil
has atype
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 callingconst pen3 = new AnotherPencil("green", "common");
– Bergi
Nov 13 '18 at 17:26
|
show 1 more comment
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
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
javascript oop
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 propertytype
becauseSomePencil
has atype
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 callingconst pen3 = new AnotherPencil("green", "common");
– Bergi
Nov 13 '18 at 17:26
|
show 1 more comment
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 propertytype
becauseSomePencil
has atype
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 callingconst 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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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
thank you! I must be more attentive
– disco_dinosaur
Nov 13 '18 at 18:54
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
thank you! I must be more attentive
– disco_dinosaur
Nov 13 '18 at 18:54
add a comment |
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
thank you! I must be more attentive
– disco_dinosaur
Nov 13 '18 at 18:54
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 propertytype
becauseSomePencil
has atype
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