Skip to content

Instantly share code, notes, and snippets.

View JasonDeving's full-sized avatar

Jason JasonDeving

View GitHub Profile
@JasonDeving
JasonDeving / Max Number with es6
Created May 1, 2016 05:37
FInd the max number with es6 with an array.
let numbers = [4,6,3,8];
let max = Math.max(...numbers);
console.log(max);
@JasonDeving
JasonDeving / READ.me
Last active March 26, 2016 04:01
Object Oriented
# Object oriented programming
```js
var answer = {
get: function fn1() {
return this.val;
},
val: 42
};
```
var counter = (function() {
var i = 0;
return {
get: function() {
return i;
},
set: function(val) {
i = val;
},
@JasonDeving
JasonDeving / Callbacks
Created March 20, 2016 02:58
Callbacks
Write a function, funcCaller, that takes a func (a function) and an arg (any data type). The function returns the func called with arg(as an argument).
var funcCaller = function(func, arg) {
return func(arg);
}
Write a function, firstVal, that takes an array, arr, and a function, func, and calls func with the first index of the arr, the index # and the whole array.
var firstVal = function(arr, func){
return func(arr[0], 0, arr);
}
Change firstVal to work not only with arrays but also objects. Since objects are not ordered, you can use any key-value pair on the object.
@JasonDeving
JasonDeving / Toaster
Created March 20, 2016 02:52
Toaster
var Toaster = function(){
//some private methods and properties
var maxTemp = 500,
temp: 0;
return {
//some public methods and properties, etc
setTemp: function(newTemp) {
if (newTemp > maxTemp) {
console.log("That temp is too high!");
} else {
@JasonDeving
JasonDeving / passing arguments as callbacks
Last active March 20, 2016 02:51
passing arguments as callbacks
var increment = function(n){ return n + 1; };
var square = function(n){ return n*n; };
var doMath = function(n, func){ return func(n); };
var squareAnswer = doMath(5, square);
var plusOne = doMath(4, increment);
console.log(squareAnswer);
console.log(plusOne);
@JasonDeving
JasonDeving / callback
Created March 20, 2016 01:54
callback
var ifElse = function(condition, isTrue, isFalse){
if(condition){
isTrue();
} else {
isFalse();
}
};
var logTrue = function(){ console.log(true); };
var logFalse = function(){ console.log(false); };
@JasonDeving
JasonDeving / module pattern
Last active March 20, 2016 01:12
module pattern
var Module = function(){
var privateProperty = 'foo';
function privateMethod(args){
// do something
};
return {
publicProperty: "",
publicMethod: function(args){
// do something
@JasonDeving
JasonDeving / closures
Created March 20, 2016 00:46
closures
var nonsense = function(string){
var blab = function(){
alert(string);
};
setTimeout(blab, 2000);
}
nonsense('blah blah');
function counter() {
var n = 0;
return {
count: function() {return ++n; },
reset: function() {n = 0;}
}
};
var myCounter = counter(), num;
myCounter.count();