Breaking the MagiJSian’s Code: JavaScript Object Creation Patterns, Revealed!

Basic Sleight of Hand: `Object.create`

Creating a ‘totally blank’ object

ES6 Classes

Pseudo-Classical

Note: use of the `constructor` property to check type is not optimal: I recommend this https://css-tricks.com/understanding-javascript-constructors/ for further discussion on the topic.
function Constructor() { 
// Add properties you want to be unique to an object
}
Constructor.prototype.propertyName = // a shared property;Constructor.prototype.methodName = function() { // a shared method }
function Constructor() { 
// Add properties you want to be unique to an object
if(!this.propertyName) {
Constructor.prototype.propertyName = // shared property;
}
if(typeof this.methodName !== 'function') {
Constructor.prototype.methodName = function() { // shared method }
}
// We checked if the properties/methods existed on 'this', (or a prototype in 'this's prototype chain).
}
For more depth, and discussion of the Object.defineProperty method from earlier: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties

Objects Linked to Other Objects (OLOO)

The splitting up of the stages of object instantiation allows for some neat ‘inheritance’ type tricks
The magician’s ultimate secret revealed

--

--

--

Ruby/JavaScript developer on the path of mastery, thanks to LaunchSchool.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

ES6 — The new Generation of JavaScript — Introduction

Apply multiple functors as arguments to a function (Applicatives)

ReactJS vs VueJS: Checking the Two Best Front-End Frameworks

GYALTHANG DZONG SHANGRI LA

Ema Hotel

Summary of Harmony questions when developing

Introduction to Cache Part 1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nick Stebbs

Nick Stebbs

Ruby/JavaScript developer on the path of mastery, thanks to LaunchSchool.

More from Medium

MY EXPERIENCE WITH JAVASCRIPT PROMISES

What is hoisting in JavaScript?

Phase 1: JavaScript

Job search update — JavaScript Fetch