Skip to content

Instantly share code, notes, and snippets.

View wgottschalk's full-sized avatar

William Gottschalk wgottschalk

  • Snap Inc.
  • Los Angeles
View GitHub Profile
@wgottschalk
wgottschalk / testing.js
Created January 14, 2016 17:54
this is a test
var test = hello world
var test = "hello world"
@wgottschalk
wgottschalk / newKeywordFunction.js
Created January 14, 2016 19:18
An implementation of the new Keyword in Javascript
function NEW(constructor, argsArray) {
var obj = {}; // step 1
obj.__proto__ = constructor.prototype; // step 2
constructor.apply(obj, argsArray); // step 3
return obj; // step 4
}
function Parent(name) {
this.name = name;
}
Parent.prototype.greet = function() {
class Parent {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hi, I'm ${this.name}`);
}
}
var mom = new Parent("Mom");
class Parent {
constructor(name) {
this.name = name;
}
}
Parent.prototype.greet = () => {
console.log(`Hi, I'm ${this.name}`);
}
var mom = new Parent("Mom");
mom.greet() // "Hi, I'm Mom"
function Parent(name) {
var obj = Object.create(parentProto);
obj.name = name;
return obj
}
var parentProto = {
greet: function() {
console.log("hi, I'm " + this.name);
}
};
function buildObject(str) {
var newObj = {};
var newKey, newVal;
var elstart = 1;
var elend = 1;
var contentsStr = str.substring(1,str.length-1);
contentsStr = contentsStr.trim();
var contentsLength = contentsStr.length;
if (contentsLength === 0) {
function buildObject(str) {
var newObj = {};
var newKey, newVal;
var elstart = 1;
var elend = 1;
var contentsStr = str.substring(1,str.length-1);
contentsStr = contentsStr.trim();
var contentsLength = contentsStr.length;
if (contentsLength === 0) {
function buildObject(str) {
var newObj = {};
var newKey, newVal;
var elstart = 1;
var elend = 1;
var contentsStr = str.substring(1,str.length-1);
contentsStr = contentsStr.trim();
var contentsLength = contentsStr.length;
if (contentsLength === 0) {
function buildObject(str) {
var newObj = {};
var contents = str.substring(1, str.length-1).split(",");
if (!contents[0]) return newObj;
contents.forEach(function(keyValuePair) {
var keyAndValue = keyValuePair.split(":");
var key = keyAndValue[0];
var value = keyAndValue[1];
newObj[key] = parse(value); // parse is a helper function