Skip to content

Instantly share code, notes, and snippets.

View easierbycode's full-sized avatar

▓▒░ ♔ Daniel ♔ ░▒▓ easierbycode

View GitHub Profile
@easierbycode
easierbycode / automagic semicolons.js
Created March 1, 2012 22:19
automagic semicolons
function laugh()
{
return
{
haha: "ha!"
};
}
laugh();
// returns undefined
// It?s treated like: var x = (y = 1); thus, ?y=1? creates an auto-global since there?s no binding ?var? statement for it. then that value gets copied into properly defined local var ?x?.
(function(){
var x = y = 1;
})();
alert(x); // undefined
alert(y); // 1 -- oops, auto-global!
@easierbycode
easierbycode / undefined is mutable.js
Created March 1, 2012 22:23
undefined is mutable
// In JavaScript, undefined is nothing but a global variable name without a default value. Therefore, its primitive value is undefined. You can change the value of undefined:
var a = {};
a.b === undefined; // true because property b is not set
undefined = 42;
a.b === undefined; // false
// Due to the mutability of undefined, it is generally a better idea to check for undefined-ness through typeof:
@easierbycode
easierbycode / jQuery callback bindings.coffee
Created March 9, 2012 14:56
jQuery callback bindings
# http://coffeescriptcookbook.com/chapters/jquery/callback-bindings-jquery
# By using the fat arrow (=>) instead of the normal arrow (->) the function gets automatically bound to the object and can access the @-variable.
$ ->
class Basket
constructor: () ->
@products = []
$('.product').click (event) =>
# http://coffeescriptcookbook.com/chapters/syntax/embedding_javascript
`function greet(name) {
return "Hello "+name;
}`
# Back to CoffeeScript
greet "Coffee"
# => "Hello Coffee"
@easierbycode
easierbycode / placeholder_polyfill.coffee
Created March 12, 2012 18:27
IE placeholder polyfill
# -= styling =-
# li label.over
# margin-top: 16px
# margin-left: 9px
# color: #777
# input[type="text"]:active, input[type="password"]:active
# color: black
adc = adc or {}
class adc.particle
bounds: null
constructor: ->
@x = Math.random() * @bounds.x2
@y = Math.random() * @bounds.y2
class adc.particleFoo extends adc.particle
@easierbycode
easierbycode / mixin.coffee
Created March 14, 2012 06:46
mixins - class/instance properties and methods
# // http://arcturo.github.com/library/coffeescript/03_classes.html
# // We're going to define a class called Module that we can inherit from for mixin support. Module will have two static functions, @extend() and @include() which we can use for extending the class with static and instance properties respectively.
moduleKeywords = ['extended', 'included']
class Module
@extend: (obj) ->
for key, value of obj when key not in moduleKeywords
@[key] = value
describe('built-in matchers', function() {
describe('toBeTruthy', function() {
it('passes if subject is true', function() {
expect(true).toBeTruthy();
expect(false).not.toBeTruthy();
});
});
describe('toBeFalsy', function() {
it('passes if subject is false', function() {
@easierbycode
easierbycode / uri.js
Created April 22, 2012 20:53 — forked from jlong/uri.js
URI Parsing with Javascript
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"