Skip to content

Instantly share code, notes, and snippets.

View jmcmaster's full-sized avatar

Jason McMaster jmcmaster

View GitHub Profile
@jmcmaster
jmcmaster / linkedlist.js
Created June 8, 2018 16:06
JS Data Structures - Linked List
function Node(data) {
this.data = data;
this.next = null;
}
function LinkedList() {
this.head = null;
}
LinkedList.prototype.addNode = function(node) {
@jmcmaster
jmcmaster / this-new-and-window.js
Created June 7, 2018 15:04
this - Part 3 - new and window binding
// 1 - Implicit binding
// 2 - Explicit binding
// 3 - new binding
// 4 - window binding
// new Binding
// this is bound to the new object being constructed
var Animal = function(color, name, type) {
//this = {}; JS does this automatically for us behind the scenes.
this.color = color;
@jmcmaster
jmcmaster / this-explicit.js
Last active June 7, 2018 14:50
this - Part 2 - Explicit Binding
// Explicit Binding
// call, apply, bind
// these methods allow you to explicitly state what the 'this' keyword is going to be in any given function
// Example 1
var sayName = function(lang1, lang2, lang3) {
console.log('My name is ' + this.name + ' and I know ' + lang1 + ', ' + lang2 + ', and ' + lang3);
};
var stacy = {
@jmcmaster
jmcmaster / this-implicit.js
Created June 7, 2018 14:06
this - Part 1 - Implicit Binding
// Implicit Binding
// Left of the dot at call time
// Ask yourself...
// where is this function invoked?
// Example 1
var me = {
name: 'Nate',
age: 25,
@jmcmaster
jmcmaster / oop.js
Created May 18, 2018 13:45
OOP in JavaScript
// Demonstrates a few ways to achieve OOP with JavaScript
// Solution 1
function playerCreator(name, score) {
var newPlayer = Object.create(playerFunctionStore);
newPlayer.name = name;
newPlayer.score = score;
return newPlayer;
}
@jmcmaster
jmcmaster / named_params_destructuring.js
Created May 11, 2018 15:06
Named Params Destructuring
function display({data, error, loading}) {
if (loading) {
return "Loading...";
}
if (error) {
return error.message;
}
return `Hello ${data.name}`;
}
var people = (function() {
var people = ['Will', 'Matt'];
// Cache Dom
$el = $('#peopleModule');
$button = this.$el.find('button');
$input = this.$el.find('input');
$ul = this.$el.find('ul');
template = this.$el.find('#people-template').html();
(function() {
var people = {
people: ['Will', 'Matt'],
template: $('#people-template').html(),
init: function() {
this.cacheDom();
this.bindEvents();
this.render();
},
// Observer Pattern
// Inspired by: https://addyosmani.com/resources/essentialjsdesignpatterns/book/#observerpatternjavascript
var Subject = function() {
let observers = [];
const publicAPI = {
subscribeObserver: function(observer) {
observers.push(observer);
// Singleton Pattern
// Inspired by: https://addyosmani.com/resources/essentialjsdesignpatterns/book/#singletonpatternjavascript
var mySingleton = (function () {
// Instance stores a reference to the Singleton
let instance;
function init() {