26
loading...
This website collects cookies to deliver better user experience
Car
object, and have them all go to separate paths and pick the best.Person
at that moment. It would be wise to create a clone of Person
first and then take its date of birth because there is a slight chance that it would be modified by some other code.class Article {
public title;
public body;
private created_at;
constructor(title, body, created_at){
this.title = title;
this.body = body;
this.created_at = created_at;
}
}
class Client {
Article article;
constructor(Article article){
this.article = article;
}
public Article duplicate(){
const clone = new Article();
clone.title = this.article.title;
clone.body = this.article.body;
}
}
Article
object and copied the title and body from the initial object to the copied one, but what about the created_at
field? Article
class, if we want to copy an object of another class, we will have to create this whole process again, but for a different class. interface Cloneable {
public clone()
}
Article
class to use this interface.class Article implements Cloneable {
public title;
public body;
private created_at;
constructor(title, body, created_at){
this.title = title;
this.body = body;
this.created_at = created_at;
}
public clone(){
const clone = new Article(this.title, this.body, this.created_at);
return clone;
}
}
Article
class now has a clone method which returns for use a clone of the current object. Client
class we can simply:class Client {
Article article;
constructor(Article article){
this.article = article;
}
public Article duplicate(){
return this.article.clone();
}
}