This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Car() { | |
this.array = []; | |
this.log = function() {} | |
} | |
Car.prototype.something = function() { | |
var self = this; // <- fine | |
this.array.forEach( function(item) { | |
self.log( item ); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* simulating constant behaviour where | |
* given value cannot be changed | |
* | |
* @param {object} target object where the constant will be defined | |
* @param {string} key | |
* @param {object} value | |
* @return {object} constant | |
* | |
* example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* string parser with data passed as arguments | |
* | |
* @param {arguments} | |
* @return {string} | |
* | |
* example: | |
* -------- | |
* 'i used both [0] & [1], however [1] looks good so far'.parse( 'iphone', 'andoird' ); // "i used both iphone & andoird, however andoird looks good so far" | |
* 'the weather now in [0] seems to be [1]'.parse( 'Dubai', function() { return 'very hot'; }); // "the weather now in Dubai seems to be very hot" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* parse given query string url, and return it as | |
* object literal with proper type casting for primitives | |
* | |
* @param {string} url | |
* @return {object} queryString | |
* | |
* notes: | |
* - requires jQuery for linear typecasting | |
* - parameter names are all lowercased |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! Js Pub/Sub | |
* http://anasnakawa.com/ | |
* Copyright (c) Anas Nakawa | |
* inspired by Ben Alman's one <https://gist.github.com/cowboy/661855> | |
* MIT License | |
*/ | |
(function( p ) { | |
var e = p.e = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tiniest Module Wrapper - (c) Anas Nakawa - <anas.nakawa {at} gmail.com> | |
// License: MIT (http://www.opensource.org/licenses/mit-license.php) | |
(function() { | |
function myLibrary() {}; | |
// CommonJs / Node | |
( typeof module !== "undefined" && module.exports && ( module.exports = myLibrary ) ) || | |
// AMD / RequireJs | |
( typeof define !== "undefined" && !define(function() { return myLibrary; }) ) || | |
// browser | |
( typeof window !== "undefined" && ( window.myLibrary = myLibrary ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point { | |
constructor( x, y ) { | |
this.x = x; | |
this.y = y; | |
} | |
plus( aPoint ) { | |
return new Point( this.x + aPoint.x, this.y + aPoint.y ); | |
} | |
distance() { | |
return Math.sqrt( this.x * this.x + this.y * this.y ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// parse a data-bind attribute | |
// | |
// @param {string} attr | |
// @return {object} parsed binding object | |
// | |
// examples: | |
// --------- | |
// parseBindings( 'something: { cool: true }' ); // { something: { cool: true } } | |
function parseBindings( attr ) { | |
return new Function( "return { " + attr + " };" )(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* jQuery Tiny Pub/Sub - v0.X - 11/18/2010 | |
* http://benalman.com/ | |
* | |
* Original Copyright (c) 2010 "Cowboy" Ben Alman | |
* Dual licensed under the MIT and GPL licenses. | |
* http://benalman.com/about/license/ | |
* | |
* Made awesome by Rick Waldron | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// adding default value when initializing an observable | |
// for later resetting, preventing the observable from | |
// becoming undefined ( still you can set it to `null` ) | |
// ==================================================== | |
// example[1]: primitive types ( passed by value ) | |
// ----------------------------------------------- | |
// var username = ko.observable().default( 'guest' ); | |
// username(); // 'guest' | |
// username( 'admin' ); // 'admin' | |
// username( undefined ); // 'guest' |