Having an object, say Book
which has a collection of other objects Page
. So I instantiate pages from raw data passed to Book
:
function Book(data){
this.pages = [];
var self = this;
data.forEach(function(item){
self.add(item);
});
}
Book.prototype.add = function(data){
this.pages.push(new Page(data));
}
function Page(data){
// some validation code
this.prop = data.prop;
}
Page.prototype...
from lectures on testability I heard that it is a bad practice to instantiate(use new
) in another object. What is the right way to do the same?
If it is okay - is there any difference if I instantiate a new Page
in add()
method or pass to it as an object already(this.add(new Page(data))
)?