I have a Person constructor and I want to add a method supposed to add friends. I want to allow my user to pass a variable number of friends so I thought about the new "rest" feature of ES6. Sadly, I can't find my way out. Here's my first try (error : "Uncaught TypeError: f.addFriends is not a function(…)"):
// Persons creator
function Person(name){
this.name = name;
this.friends = [];
this.addFriends = function(...a){
a.forEach(function(d){this.friends.push(d)});
}
}
// Create three persons
f = new Person("Fanny");
e = new Person("Eric");
j = new Person("John");
// add Eric & Fanny as friends of Fanny
f.addFriends(e,j);
I've also tried the following code (no error, but no friends added):
// Persons creator
function Person(name){
this.name = name;
this.friends = [];
}
Person.prototype.addFriends = function(...a){
a.forEach(function(d){this.friends.push(d)});
}
// Create three persons
f = new Person("Fanny");
e = new Person("Eric");
j = new Person("John");
// add Eric & Fanny as friends of Fanny
f.addFriends(e,j);
What am I doing wrong? Many thanks for your help!