Skip to content

Instantly share code, notes, and snippets.

View icodesido's full-sized avatar

Ivan icodesido

View GitHub Profile
@icodesido
icodesido / Bitwise Xor
Created October 17, 2014 10:15
Toggle with Xor
var x = 0;
// this will toggle between 0 and 1 everytime is reset
x ^= 1;
@icodesido
icodesido / Better than .push
Created October 17, 2014 10:46
Better performing alternative to push
var x = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'],
y = [];
for (var i = 0; i < x.length; i++) {
y[y.length] = x[i];
}
@icodesido
icodesido / jQuery plugin boilerplate
Created October 17, 2014 15:51
jQuery plugin template
(function($){
$.fn.yourPluginName = function(){
// Your code goes here
return this;
};
})(jQuery);
@icodesido
icodesido / Cl cl closures
Created October 20, 2014 15:38
Getting closure
var nodes = document.getElementsByTagName('*');
for (var i = nodes.length - 1; i >= 0; i--) {
nodes[i].addEventListener('click', function() {
console.log('You clicked element #' + i);
});
}
var nodes = document.getElementsByTagName('*');
for (var i = nodes.length - 1; i >= 0; i--) {
nodes[i].addEventListener('click', (function(i) {
@icodesido
icodesido / JSON toJSON
Created October 27, 2014 12:41
toJSON as a method in an object changes the behaviour of JSON
var obj = {
foo: 'foo',
toJSON: function () {
return 'bar';
}
};
JSON.stringify(obj); // '"bar"'
JSON.stringify({x: obj}); // '{"x":"bar"}'
@icodesido
icodesido / Reduce to stop Abuse
Created October 27, 2014 12:58
Instead of the double array conundrum, use a memo and reduce.
// abuse
var orig = [...];
var stuff = [];
_.each(orig, function (item) {
var t = // do something to item
// Mutate somebody else's thing
stuff.push(t);
};
// reduce
@icodesido
icodesido / SASS mixins
Created November 17, 2014 12:38
Essential SASS mixins
1 - BOX SIZING
@mixin box-sizing($type)
{
-webkit-box-sizing:$type;
-moz-box-sizing:$type;
box-sizing:$type;
}
div {
@include box-sizing(border-box);
@icodesido
icodesido / Mixin example
Last active August 29, 2015 14:14
Sass mixin with arguments and default value from variable
$base-color: orange;
@mixin headline($size, $color: $base-color) {
color: $color;
font-size: $size;
}
h1 {
@include headline(12px);
}
@icodesido
icodesido / Sass Overloaded Args
Last active August 29, 2015 14:14
Sass overloading arguments
@mixin pad ($pads...) {
padding: $pads;
}
.one {
@include pad(20px);
}
.two {
@include pad(10px 20px);
}
@icodesido
icodesido / SASS @ content
Created January 27, 2015 11:51
SASS @content example
@mixin cont {
background-color: black;
color: white;
@content;
}
div {
@include cont {
font-size: 12px;
font-style: italic;