1024px-Flatirons_Winter_Sunrise_edit_2

Instead of creating objects with the new keyword, there is an alternative way to create objects using the Object.create method that is new as of ECMAScript 5 (also referred to as ES5).

Thankfully, there is also a pollyfill for browsers that don’t yet support the native Object.create method. [Douglas Crockford](http://javascript.crockford.com/prototypal.html) posted the following code snippet to provide backwards compatibility for older browsers.

if (typeof Object.create !== ‘function’) {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
newObject = Object.create(oldObject);

##Without a Constructor

When using Object.create you don’t have to create a constructor at all to get started. All you really need is an object that you want to inherit from. Let’s take a look at another C# example and show how we can rewrite it using the Object.create syntax.

The following is a example C# snippet that we’ve used in the past couple of articles in this series.

using System;

public class Skillet
{
public string Ingredient { get; set; }
public int Quantity { get; set; }

public Skillet() {
this.Ingredient = “sticks of butter”;
this.Quantity = 2;
}

public Skillet( string ingredient, int quantity )
{
this.Ingredient = ingredient;
this.Quantity = quantity;
}

public virtual void fry()
{
Console.WriteLine( “Frying ” + this.Quantity + ” ” + this.Ingredient );
}
}

public class BaconSkillet : Skillet {
public BaconSkillet() : base() {}

public BaconSkillet( string ingredient, int quantity ) : base( ingredient, quantity ) {}

public override void fry()
{
base.fry();
Console.WriteLine( “Frying ” + this.Quantity + ” crispy ” + this.Ingredient );
}
}

public class Runner
{
public static void Main()
{
Skillet genericSkillet = new Skillet();
genericSkillet.fry(); //Frying 2 sticks of butter

BaconSkillet baconSkillet = new BaconSkillet( “bacon strips”, 4 );
baconSkillet.fry(); //Frying 4 bacon strips /n Frying 4 crispy bacon strips
}
}
https://ideone.com/IE0CK

http://jsfiddle.net/elijahmanor/R2NvS/8/

##With a Constructor

Since all you need to use Object.create is an existing object, you can also used it in conjuction with an object created using a custom Constructor with the new keywword.

[example code here]

##Combining with the Revealed Module Pattern

A common pattern made popular by Douglas Crockford is the Module Pattern allowing you to define public and proviate methods and properties inside your object. The Revealed Module Pattern is a slight derivation of the pattern that uses a slightly easier syntax to define.

By using the Object.create method we can create new instances of an object based off of the encapsulated nature of the Revealed Module Pattern. Inside the new object our pubic parts will remain public and our private parts will remain private, just as we defined when we created the initial object.

[example code here…

var skillet = (function() {
var pub = {},
//Private property
amountOfGrease = “1 Cup”;

//Public property
pub.ingredient = “Bacon Strips”;

//Public method
pub.fry = function() {
console.log( “Frying ” + pub.ingredient );
};

//Private method
function privateWay() {
//Do something…
}

//Return just the public parts
return pub;
}());

var anotherSkillet = Object.create( skillet );

anotherSkillet.fry();

…]

##Pros of This Technique

*

*

*

*

##Cons of This Technique

*