Instantiation Patterns in JS

There are 4 ways to create an object in JS. Either way gives you...

The 4 patterns are...
  1. Functional
  2. Functional-shared
  3. Prototypal
  4. Pseudoclassical

Functional

...

var Skater = function(type, name) {
	var obj = {};
	obj.type = type;
	obj.name = name;

	obj.makeSound = function() {
		//code here
	}
	obj.eat = function() {
		console.log(`${this.name} is eating`)
	}
	obj.sleep = function() {

	}
	return obj;
}
//implementation of functional instantiation
var viking = Skater('vert', 'renee');
var god = Skater('street', 'tomFry')
viking.eat();
god.eat();

Pros

Cons

Functional-shared

So now we see a style that gives us methods that are shared across objects.

var spot = function(city, burly) {
	var obj = {};

	obj.city = city;
	obj.burly = burly; 

	extend(obj, objMethods);

	return obj;
}

var extend = function(obj, methods) {
	for(var key in methods) {
		obj[key] = methods[key];
	}
}

var objMethods = {
	makeSound: function() {
		console.log(`${this.city} is my favorite place`);
	},
	eat: function() {
		// eat code here
	}, 
	sleep: function() {
		//sleep code here
	}
}
//and now the implementation
var hHigh = spot('chicago', 'small');
hHigh.makeSound();

Prototypal

In this case we will use the prototypal chain.
	
//here is a prototypal instantiation
var Dog = function(variety, namez) {
  
  var obj = Object.create(objMethods);
  obj.variety = variety;
  obj.namez = namez;

  return obj;
}
var objMethods = {
	makeSound: function() {
		console.log(`${this.namez} says woof`)
	},
	eat: function() {
		//eat code here
	},
	sleep: function() {
		//sleep code here
	}
}
//implement prototypal instantiation
var pooties = Dog('poodle', 'sam');
pooties.makeSound(); //sam says woof
})();
	

Pseudoclassical style

	
var skater = function(city, name) {

	this.city = city;
	this.name = name;

}

skater.prototype.whereAt = function() {
	//code goes here.
}

skater.prototype.eat = function() {
	console.log(`${this.name} eats pizza`);
}

//and pseudoclassical implementation is...
var cortez = new skater('long beach', 'steven');
cortez.eat(); //steven eats pizza
})();
	

Now to be honest this feels a bit silly. Javascript is NOT a language built on classical inheritance. It is prototypal. This "new" keyword is here to make our hand-written code shorter? really? I'm missing something here. I believe I heard Crockford say that it's really just a new pattern for people that are used to programming in languages with classical inheritance like Java. As a beginner, this is really confusing. Wouldn't you rather be working as the language really is instead of how you want it to be??

All of this code is from a medium article someone else wrote. I am copying it for the simple practice of it. I will stick with prototypal inheritance for now (as of august 2017)