How to spread the parameters of a previous instance of a class constructor to another instance
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I would like to ask what I am doing wrong here
My goal
I want to create instances from a class constructor.
The first is gonna be a more generic class called Person and then another that will inherit properties from that class.
My question is
When all classes are set and the first instance that points to the Person constructor is declared, how could the pass the key: values
of the previous instance to the next instance since I don't want to repeat my self over the same arguments.
I am currently spreading the previous parameters of the instance but obviously, I am doing something wrong.
class Person {
constructor (name,yearOfBirth,job) {
this.name = name;
this.yearOfBirth = yearOfBirth;
this.job = job;
}
getAge() {
return new Date().getFullYear() - this.yearOfBirth
}
greet(){
return `${this.name} is a ${this.getAge()} years old ${this.job}`
}
}
class footballPlayer extends Person {
constructor(name,yearOfBirth, job, team, cups) {
super(name, yearOfBirth, job)
this.team = team;
this.cups = cups;
}
cupsWon() {
console.log(`${this.name} who was bord on ${this.year} and works as a ${this.job} won ${this.cups} with ${this.team}`);
}
}
const vagg = new Person('vaggelis', 1990, 'Developer');
const vaggA= new footballPlayer( {...vagg} , 'real madrid', 4)
console.log(vagg.greet());
console.log(vaggA.cupsWon());
Thank you!
javascript constructor spread-syntax class-constructors
add a comment |
I would like to ask what I am doing wrong here
My goal
I want to create instances from a class constructor.
The first is gonna be a more generic class called Person and then another that will inherit properties from that class.
My question is
When all classes are set and the first instance that points to the Person constructor is declared, how could the pass the key: values
of the previous instance to the next instance since I don't want to repeat my self over the same arguments.
I am currently spreading the previous parameters of the instance but obviously, I am doing something wrong.
class Person {
constructor (name,yearOfBirth,job) {
this.name = name;
this.yearOfBirth = yearOfBirth;
this.job = job;
}
getAge() {
return new Date().getFullYear() - this.yearOfBirth
}
greet(){
return `${this.name} is a ${this.getAge()} years old ${this.job}`
}
}
class footballPlayer extends Person {
constructor(name,yearOfBirth, job, team, cups) {
super(name, yearOfBirth, job)
this.team = team;
this.cups = cups;
}
cupsWon() {
console.log(`${this.name} who was bord on ${this.year} and works as a ${this.job} won ${this.cups} with ${this.team}`);
}
}
const vagg = new Person('vaggelis', 1990, 'Developer');
const vaggA= new footballPlayer( {...vagg} , 'real madrid', 4)
console.log(vagg.greet());
console.log(vaggA.cupsWon());
Thank you!
javascript constructor spread-syntax class-constructors
You cannot spread an object into an arguments list. You should make a constructor that takes an object instead.
– Bergi
Nov 22 '18 at 13:23
add a comment |
I would like to ask what I am doing wrong here
My goal
I want to create instances from a class constructor.
The first is gonna be a more generic class called Person and then another that will inherit properties from that class.
My question is
When all classes are set and the first instance that points to the Person constructor is declared, how could the pass the key: values
of the previous instance to the next instance since I don't want to repeat my self over the same arguments.
I am currently spreading the previous parameters of the instance but obviously, I am doing something wrong.
class Person {
constructor (name,yearOfBirth,job) {
this.name = name;
this.yearOfBirth = yearOfBirth;
this.job = job;
}
getAge() {
return new Date().getFullYear() - this.yearOfBirth
}
greet(){
return `${this.name} is a ${this.getAge()} years old ${this.job}`
}
}
class footballPlayer extends Person {
constructor(name,yearOfBirth, job, team, cups) {
super(name, yearOfBirth, job)
this.team = team;
this.cups = cups;
}
cupsWon() {
console.log(`${this.name} who was bord on ${this.year} and works as a ${this.job} won ${this.cups} with ${this.team}`);
}
}
const vagg = new Person('vaggelis', 1990, 'Developer');
const vaggA= new footballPlayer( {...vagg} , 'real madrid', 4)
console.log(vagg.greet());
console.log(vaggA.cupsWon());
Thank you!
javascript constructor spread-syntax class-constructors
I would like to ask what I am doing wrong here
My goal
I want to create instances from a class constructor.
The first is gonna be a more generic class called Person and then another that will inherit properties from that class.
My question is
When all classes are set and the first instance that points to the Person constructor is declared, how could the pass the key: values
of the previous instance to the next instance since I don't want to repeat my self over the same arguments.
I am currently spreading the previous parameters of the instance but obviously, I am doing something wrong.
class Person {
constructor (name,yearOfBirth,job) {
this.name = name;
this.yearOfBirth = yearOfBirth;
this.job = job;
}
getAge() {
return new Date().getFullYear() - this.yearOfBirth
}
greet(){
return `${this.name} is a ${this.getAge()} years old ${this.job}`
}
}
class footballPlayer extends Person {
constructor(name,yearOfBirth, job, team, cups) {
super(name, yearOfBirth, job)
this.team = team;
this.cups = cups;
}
cupsWon() {
console.log(`${this.name} who was bord on ${this.year} and works as a ${this.job} won ${this.cups} with ${this.team}`);
}
}
const vagg = new Person('vaggelis', 1990, 'Developer');
const vaggA= new footballPlayer( {...vagg} , 'real madrid', 4)
console.log(vagg.greet());
console.log(vaggA.cupsWon());
Thank you!
javascript constructor spread-syntax class-constructors
javascript constructor spread-syntax class-constructors
edited Jan 17 at 14:02
Praveen Kumar Purushothaman
135k23141189
135k23141189
asked Nov 22 '18 at 11:54
EvanEvan
818
818
You cannot spread an object into an arguments list. You should make a constructor that takes an object instead.
– Bergi
Nov 22 '18 at 13:23
add a comment |
You cannot spread an object into an arguments list. You should make a constructor that takes an object instead.
– Bergi
Nov 22 '18 at 13:23
You cannot spread an object into an arguments list. You should make a constructor that takes an object instead.
– Bergi
Nov 22 '18 at 13:23
You cannot spread an object into an arguments list. You should make a constructor that takes an object instead.
– Bergi
Nov 22 '18 at 13:23
add a comment |
1 Answer
1
active
oldest
votes
If I understand correctly what you want to do, you need to pass only the values of the parameters that describe the Person
to the footballPlayer
(note: class names should by convention be uppercase).
var vaggA = new footballPlayer( ...Object.values(vagg) , 'real madrid', 4);
Edit: In case you fear a different order with Object.values
(which is a real threat), you can create a getter function in the Person
class that will return the exact list of parameters in the order they are supposed to be given to the constructor:
class Person {
// ...
describe() {
return [this.name, this.yearOfBirth, this.job];
}
}
const vaggA = new footballPlayer( ...vagg.describe() , 'real madrid', 4);
Object.values
does not guarantee order, so this might easily break
– Bergi
Nov 22 '18 at 13:20
That's not true regardingString
keys in 2018. This can be discussed but no need to downvote IMO.
– Zim
Nov 22 '18 at 13:39
Unfortunately the answer you linked is simply wrong. See the one I linked with quotes from the specification
– Bergi
Nov 22 '18 at 14:02
Your link tells the same about String keys: "Other string keys (if applicable), in property creation order."
– Zim
Nov 22 '18 at 14:04
1
I've edited my answer to take into account your statements. Thanks for having me get aware of this.
– Zim
Nov 22 '18 at 14:26
|
show 2 more comments
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%2f53430473%2fhow-to-spread-the-parameters-of-a-previous-instance-of-a-class-constructor-to-an%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
If I understand correctly what you want to do, you need to pass only the values of the parameters that describe the Person
to the footballPlayer
(note: class names should by convention be uppercase).
var vaggA = new footballPlayer( ...Object.values(vagg) , 'real madrid', 4);
Edit: In case you fear a different order with Object.values
(which is a real threat), you can create a getter function in the Person
class that will return the exact list of parameters in the order they are supposed to be given to the constructor:
class Person {
// ...
describe() {
return [this.name, this.yearOfBirth, this.job];
}
}
const vaggA = new footballPlayer( ...vagg.describe() , 'real madrid', 4);
Object.values
does not guarantee order, so this might easily break
– Bergi
Nov 22 '18 at 13:20
That's not true regardingString
keys in 2018. This can be discussed but no need to downvote IMO.
– Zim
Nov 22 '18 at 13:39
Unfortunately the answer you linked is simply wrong. See the one I linked with quotes from the specification
– Bergi
Nov 22 '18 at 14:02
Your link tells the same about String keys: "Other string keys (if applicable), in property creation order."
– Zim
Nov 22 '18 at 14:04
1
I've edited my answer to take into account your statements. Thanks for having me get aware of this.
– Zim
Nov 22 '18 at 14:26
|
show 2 more comments
If I understand correctly what you want to do, you need to pass only the values of the parameters that describe the Person
to the footballPlayer
(note: class names should by convention be uppercase).
var vaggA = new footballPlayer( ...Object.values(vagg) , 'real madrid', 4);
Edit: In case you fear a different order with Object.values
(which is a real threat), you can create a getter function in the Person
class that will return the exact list of parameters in the order they are supposed to be given to the constructor:
class Person {
// ...
describe() {
return [this.name, this.yearOfBirth, this.job];
}
}
const vaggA = new footballPlayer( ...vagg.describe() , 'real madrid', 4);
Object.values
does not guarantee order, so this might easily break
– Bergi
Nov 22 '18 at 13:20
That's not true regardingString
keys in 2018. This can be discussed but no need to downvote IMO.
– Zim
Nov 22 '18 at 13:39
Unfortunately the answer you linked is simply wrong. See the one I linked with quotes from the specification
– Bergi
Nov 22 '18 at 14:02
Your link tells the same about String keys: "Other string keys (if applicable), in property creation order."
– Zim
Nov 22 '18 at 14:04
1
I've edited my answer to take into account your statements. Thanks for having me get aware of this.
– Zim
Nov 22 '18 at 14:26
|
show 2 more comments
If I understand correctly what you want to do, you need to pass only the values of the parameters that describe the Person
to the footballPlayer
(note: class names should by convention be uppercase).
var vaggA = new footballPlayer( ...Object.values(vagg) , 'real madrid', 4);
Edit: In case you fear a different order with Object.values
(which is a real threat), you can create a getter function in the Person
class that will return the exact list of parameters in the order they are supposed to be given to the constructor:
class Person {
// ...
describe() {
return [this.name, this.yearOfBirth, this.job];
}
}
const vaggA = new footballPlayer( ...vagg.describe() , 'real madrid', 4);
If I understand correctly what you want to do, you need to pass only the values of the parameters that describe the Person
to the footballPlayer
(note: class names should by convention be uppercase).
var vaggA = new footballPlayer( ...Object.values(vagg) , 'real madrid', 4);
Edit: In case you fear a different order with Object.values
(which is a real threat), you can create a getter function in the Person
class that will return the exact list of parameters in the order they are supposed to be given to the constructor:
class Person {
// ...
describe() {
return [this.name, this.yearOfBirth, this.job];
}
}
const vaggA = new footballPlayer( ...vagg.describe() , 'real madrid', 4);
edited Nov 22 '18 at 14:23
answered Nov 22 '18 at 13:11
ZimZim
1,0741817
1,0741817
Object.values
does not guarantee order, so this might easily break
– Bergi
Nov 22 '18 at 13:20
That's not true regardingString
keys in 2018. This can be discussed but no need to downvote IMO.
– Zim
Nov 22 '18 at 13:39
Unfortunately the answer you linked is simply wrong. See the one I linked with quotes from the specification
– Bergi
Nov 22 '18 at 14:02
Your link tells the same about String keys: "Other string keys (if applicable), in property creation order."
– Zim
Nov 22 '18 at 14:04
1
I've edited my answer to take into account your statements. Thanks for having me get aware of this.
– Zim
Nov 22 '18 at 14:26
|
show 2 more comments
Object.values
does not guarantee order, so this might easily break
– Bergi
Nov 22 '18 at 13:20
That's not true regardingString
keys in 2018. This can be discussed but no need to downvote IMO.
– Zim
Nov 22 '18 at 13:39
Unfortunately the answer you linked is simply wrong. See the one I linked with quotes from the specification
– Bergi
Nov 22 '18 at 14:02
Your link tells the same about String keys: "Other string keys (if applicable), in property creation order."
– Zim
Nov 22 '18 at 14:04
1
I've edited my answer to take into account your statements. Thanks for having me get aware of this.
– Zim
Nov 22 '18 at 14:26
Object.values
does not guarantee order, so this might easily break– Bergi
Nov 22 '18 at 13:20
Object.values
does not guarantee order, so this might easily break– Bergi
Nov 22 '18 at 13:20
That's not true regarding
String
keys in 2018. This can be discussed but no need to downvote IMO.– Zim
Nov 22 '18 at 13:39
That's not true regarding
String
keys in 2018. This can be discussed but no need to downvote IMO.– Zim
Nov 22 '18 at 13:39
Unfortunately the answer you linked is simply wrong. See the one I linked with quotes from the specification
– Bergi
Nov 22 '18 at 14:02
Unfortunately the answer you linked is simply wrong. See the one I linked with quotes from the specification
– Bergi
Nov 22 '18 at 14:02
Your link tells the same about String keys: "Other string keys (if applicable), in property creation order."
– Zim
Nov 22 '18 at 14:04
Your link tells the same about String keys: "Other string keys (if applicable), in property creation order."
– Zim
Nov 22 '18 at 14:04
1
1
I've edited my answer to take into account your statements. Thanks for having me get aware of this.
– Zim
Nov 22 '18 at 14:26
I've edited my answer to take into account your statements. Thanks for having me get aware of this.
– Zim
Nov 22 '18 at 14:26
|
show 2 more comments
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%2f53430473%2fhow-to-spread-the-parameters-of-a-previous-instance-of-a-class-constructor-to-an%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
You cannot spread an object into an arguments list. You should make a constructor that takes an object instead.
– Bergi
Nov 22 '18 at 13:23