Navigate with keyboard arrows in all directions.
Use the control on the bottom right part as page availability hint.
item = createLike(model);
Before seeing how 'createLike' method looks like lets see some theory...
Shall we classify these objects somehow? See next...
Lets give some explanation...
The simplest way to create DataHolder object is with object literal
var address = { street: "Sunset", number: 5 } assertEquals("Sunset", address.street); assertEquals(5, address.number);
At some point in time we could decide to turn 'address' Data Holder object into a Model object. See the Model object definition next...
var address = { street: "Sunset", number: 5 } assertEquals("Sunset", address.street); assertEquals(5, address.number);
Our address object could become a model for creation of more objects e.g. kind of extended addresses. Two questions arise...
How to define Creator object?
How to tell him to create objects using 'address' as a model?
In JavaScript Function Objects are Creator Objects
//Define Creator Object function ExtendedAddress () { this.zipCode = 555; }; //Create Data Holder Object var extendedAddress = new ExtendedAddress(); assertEquals(555, extendedAddress.zipCode);
How to define Creator object? -> With JavaScript constructor function
How to tell him to create objects using address as a model? See next...
var address = { street: "Sunset", number: 5 }; function ExtendedAddress () { this.zipCode = 555; } //Here we define the Model that this Creator will use ExtendedAddress.prototype = address; var extAddress = new ExtendedAddress(); assertEquals(555, extAddress.zipCode); assertEquals("Sunset", extAddress.street); assertEquals(5, extAddress.number);
//Define Creator Object function ExtendedAddress () { this.zipCode = 555; }; //Create Data Holder Object var extendedAddress = new ExtendedAddress(); assertEquals(555, extendedAddress.zipCode);
What is the default Model that ExtendedAddress is using? Answer: JavaScript automatically creates object with the same name (ExtendedAddress) and use it as a Model (assign it to Creator Object 'prototype' property)
Don't confuse the Creator and the Model. Have in mind that:
assertFalse(ExtendedAddress === ExtendedAddress.prototype)
function CreatorObject(label){ this.label = label || "DefaultDataHolderLabel"; } var creatorObject = CreatorObject; creatorObject.label = "creatorObjectLabel"; var modelObject = CreatorObject.prototype; modelObject.label = "modelObjectLabel"; var dataHolderObject1 = new CreatorObject(); var dataHolderObject2 = new CreatorObject("ConcreteDataHolderLabel"); assertEquals(modelObject.label, "modelObjectLabel"); assertEquals(creatorObject.label, "creatorObjectLabel"); assertEquals(dataHolderObject1.label, "DefaultDataHolderLabel"); assertEquals(dataHolderObject2.label, "ConcreteDataHolderLabel");
Here is how 'createLike' method looks like
function createLike(o) { //Define Creator Object function F() {} //Set the Model Object F.prototype = o; //Create Data Holder based on the Model return new F(); };
See example on the next slide...
var address = {street: "Sunset", number: 5}; function ExtendedAddress () {this.zipCode = 555;} //Here we define the Model that this Creator will use ExtendedAddress.prototype = address; var extAddress = new ExtendedAddress(); assertEquals(555, extAddress.zipCode); assertEquals("Sunset", extAddress.street); assertEquals(5, extAddress.number); //Add own data extAddress.newOwnProp = 1; //Add more data into Model and it will be visible address.newInheritedProp = 2; assertEquals(1, extAddress.newOwnProp); assertEquals(2, extAddress.newInheritedProp);
Pro JavaScript Design Patterns Book