In this article, we will discuss the Backbone.js clone model. The Backbone.js clone is used to provide a copy from the given model. we can also copy the model to another using clone() method.
Syntax:
Backbone.Model.clone()
Note: It takes no parameters.
Example 1: In this example, we will copy the book model to the chapter model.
HTML
<!DOCTYPE html> < html >   < head >             type = "text/javascript" ></ script >     < script src =         type = "text/javascript" ></ script >     < script src =         type = "text/javascript" ></ script >       < script type = "text/javascript" >         var Books = Backbone.Model.extend();         var book = new Books({ book_name: "HTML", price: 100 });         document.write("Values in book model : ",                        JSON.stringify(book));         document.write("< br >");           // Copy details to chapters using clone() method         var chapters = book.clone();           document.write();           document.write(           "Values in chapters model (copied from book model) : ",           JSON.stringify(chapters));     </ script > </ head >   < body ></ body >   </ html > |
Output:
Values in book model : {"book_name":"HTML","price":100} Values in chapters model (copied from book model) : {"book_name":"HTML","price":100}
Example 2: In this example, we will copy the book model to again the book model.
HTML
<!DOCTYPE html> < html >   < head >     < script src =             type = "text/javascript" ></ script >     < script src =         type = "text/javascript" ></ script >     < script src =         type = "text/javascript" ></ script >     < script type = "text/javascript" >         var Books = Backbone.Model.extend();         var book = new Books({ book_name: "css" });         document.write("Values in book model : ",                        JSON.stringify(book));         document.write("< br >");           // Copy details to book again using clone() method         var book = book.clone();           document.write();           document.write(           "Values in book model (copied from book model) : ",           JSON.stringify(book));       </ script > </ head >   < body ></ body >   </ html > |
Output:
Values in book model : {"book_name":"css"} Values in book model (copied from book model) : {"book_name":"css"}
Reference: https://backbonejs.org/#Model-clone