Skip to content

Instantly share code, notes, and snippets.

View sean-roberts's full-sized avatar

Sean Roberts sean-roberts

View GitHub Profile
@sean-roberts
sean-roberts / 0_reuse_code.js
Created August 1, 2014 17:43
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
<script>
var Constants = {
saveURL: 'http://random/birthday/saveimage.php',
w: 500,
h: 500,
x: 200,
y: 200
};
@sean-roberts
sean-roberts / SelfInvokingFunctionWrapper.js
Created May 31, 2013 01:14
Safe self invoking function wrapper! Suuhhweet! This is actually a way to create a type of Namespace in Javascript
//version without passing in parameters that are not available in the function scope
;(function(){
//code here
//why the leading semi colon?
// this is a safe way to keep from having conflicts with other code that uses the same
// self invoking functions but left off the colon
})();
@sean-roberts
sean-roberts / WrapperObject.js
Last active December 18, 2015 02:28
Wrapper Objects in JS : literal variables of type string, number, and boolean are not objects but they seem to have properties, why is that? Because, when they are accessed, JS converts the value into its object type temporarily. Then once the method or property is resolved, this new object is discarded. This temporary object is the wrapper obje…
//start with a string literal
var a = "Hello, World";
//add a new property to that string
a.dateCreated = "6-4-2013";
//try to access that property
var b = a.dateCreated;
//b is still undefined
@sean-roberts
sean-roberts / NestedFunctionScope.js
Created June 20, 2013 03:48
Nested Function's Scope : An interesting thing to note about nested functions in javascript is that they have access to the parent functions variables and parameters. This is unlike variables defined at the global level
foo(1);
function foo(a){
var b = 2;
bar(3);
function bar(c){
//all parent variables are visible and editable from here
console.log(a);
@sean-roberts
sean-roberts / gist:6546754
Created September 13, 2013 04:21
Notes from the Eric Elliott talk from OReilly Fluent conference.
/*
Notes from Eric Elliott's talk at the fluent conference
JavaScript has FREEDOM.
In classical inheritance you have tight coupling brought on by the parent-child relationship. This is good in some areas but as the project grows, making changes to the parent that ripple through the descendants can be a big burden and error/bug prone. Thus, the need for duplication by necessity. This is the situation where you have a class that does almost everything you want but it needs to do something a little differently so you create a very identical class for your new need. This is caused by the gorilla banana problem. Where you want a banana but what you got was a gorilla holding the banana and the entire jungle. The problem with object-oriented languages is they've got all this implicit environment that they carry around with them.
JavaScript has inheritance that does not have these problems.
"Program to an interface, not an implementation."
"Favor object composition over class inheritance."
@sean-roberts
sean-roberts / ObjectCreation.js
Last active December 23, 2015 04:29
This is a few of the ways to create an object in JavaScript
/*
The different ways to create an object
*/
/*
Example object needs to have this heirachy
@sean-roberts
sean-roberts / PublishSubscribeImpl.js
Last active December 25, 2015 18:09
PublishSubscriberPattern Implementation - basic
// test gist http://jsfiddle.net/fJPkr/1/
var handler = {};
(function(h){
//holds the topics
var topics = {},
subUid = 0;
h.publish = function(topic, args){
//only publish topics that were subscribed to
@sean-roberts
sean-roberts / FunctionParametersAndArgs.js
Created June 20, 2013 17:04
Function Parameter and Arguments in JS: There are special things to consider when you pass a function more or less parameters than expected because JavaScript does not consider type nor does it worry about length. When you pass 2 parameters to a function expecting 4, the last two in the arguments are set to undefined. When you pass more paramete…
//this foo function is really saying,
//"Hey, I am expecting about 3 arguments but give me what you can"
function foo(a, b, c){
console.log(a,b,c);
}
//lets ask foo to take more and less parameters than expected
foo("anArg"); // logs in console => anArg undefined undefined