Skip to content

Instantly share code, notes, and snippets.

// Both ways to create functions use the function keyword, so the difference between them aren't immediately apparent.
//******************
// Declarations
//******************
// Variable declarations and functions declarations operate similiarly. They start with a keywords (var or function), followed by an identifier.
// Function declarations add lists of arguments in parentheses followed by blocks in curly braces.
function functionName(arg1, arg2, argN) {
@khamiltonuk
khamiltonuk / TextContentVsInnerText.js
Created February 16, 2014 21:47
js functions that require css
//***********************************
// TextContent vs innerText
//***********************************
// Innertext returns with a line breaks in it because it behaves like you'd copy and pasted the element, and becasue div is block level it put "World" on a new line. innerText is styles dependant, so will therefore have to check the style sheet
var div = document.createElement('div');
div.style.background = 'url(img.png)';
div.innerHTML = "hello <div>world</div>";
document.body.appendChild(div);
@khamiltonuk
khamiltonuk / SetIntervalcanbeajerk.js
Created February 21, 2014 06:36
Time based repetition
//Alternative to setInterval
//Set interval will run the function doStuff() every 100ms regardless if doStuff has finished.
setInterval(function(){
doStuff();
},100);
//Alternatively you can use argements.callee after doStuff(); which will is wrapped in a self invoking anyonomous self invoking function
(function(){
@khamiltonuk
khamiltonuk / thatEqualsThis.js
Created March 5, 2014 17:06
Understanding This
// Bad
var myObj = {
specialFunction: function () {
},
anotherSpecialFunction: function () {
},
JavaScript has function scope only
javascript is compiled
tokens/lexing?
declarations are compiled
implicit declation argument
start using strict mode, es6 ***NEW RULE***
always name your function
so you canc call the function within its self, purpose of recusion
@khamiltonuk
khamiltonuk / SassMeister-input.scss
Created March 31, 2014 14:33
Generated by SassMeister.com.
// ----
// Sass (v3.3.4)
// Compass (v1.0.0.alpha.18)
// ----
.icon-country_x32 {
background-image: url(../images/header/spritesheet_x32.png);
display:block;
width: 48px;
height: 32px;
@khamiltonuk
khamiltonuk / sort.js
Created June 11, 2014 07:46
Sorting an array
var numbers = ['1','5','4','3','53','16','8'];
var accending = function(first,second){
return first - second;
}
var decending = function(first,second){
return second - first;
};
console.log(numbers.sort(decending))
@khamiltonuk
khamiltonuk / addsub.js
Created June 11, 2014 07:58
Adding and subtracting all the arguments in a function
function sum(){
var result = 0,
i = 0,
len = arguments.length;
while (i < len){
result += arguments[i];
i++;
}
return result;
@khamiltonuk
khamiltonuk / batmansPhoneBook
Created July 30, 2014 07:04
Lists of Unique Values - Using ES6 Sets in JavaScript
/*
** Pushing values to an array
*/
var heroCallList = [ ];
heroCallList.push("Robin");
heroCallList.push("Batgirl");
heroCallList.push("Nightwing");
console.log(heroCallList);
@khamiltonuk
khamiltonuk / helloworld.js
Created July 14, 2015 06:09
helloworld.js
var http = require('http');
http.createServer(function(req,res){
// normalize url by removing querystring, optional
var path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();
switch(path){
case '':
res.writeHead(200, {'Content-type': 'text/plain'});
res.end('Homepage');
break;