Posts Tagged “JavaScript”

Underscore.js Basics

Posted on May 10th, 2013 by webninjataylor

Underscore.js is a tiny JavaScript utility library that makes working with some of the common data structures used in JavaScript much easier Common Methods _.union(myArray2, myArray3) to concatenate two arrays _.uniq(myArray3) to strip duplicates from array _.zip(myArray, myArray2) to return arrays where the nth value of each array is paired [0, 0], [1, 1], [2, […]

HTML5 Local Storage via JavaScript

Posted on May 7th, 2013 by webninjataylor

Save to local storage via .setItem() with a key/value pair localStorage.setItem(‘my_key’, ‘array,string,whatever’); Pull from local storage via .getItem() using the key localStorage.getItem(‘my_key’);

Grunt

Posted on May 1st, 2013 by webninjataylor

Install the Grunt command line tool (requires node.js installed on your machine)… sudo npm install grunt-cli -g Create package.json in root of project (list of Grunt plugin dependencies) npm install (install the dependencies) Create Gruntfile.js in root of project (configuration) Basically, you need three different functions to work with Grunt: grunt.initConfig, grunt.loadNpmTasks, and grunt.registerTask Resources Grunt: A Build Tool […]

JavaScript Pattern: Revealing Module

Posted on April 30th, 2013 by webninjataylor

This pattern is essentially the same as the Module Pattern, except all of the functions and variables are defined in the private scope, and we return an object with pointers to the private functionality that we wish to make public. var myRevealingModule = function () { var privateCounter = 0; function privateFunction() { privateCounter++; } […]

Tags:
Posted in Web ShotsComments Off on JavaScript Pattern: Revealing Module

Decimals in JavaScript

Posted on April 30th, 2013 by webninjataylor

Use decimals cautiously because JavaScript uses Binary Floating Point numbers, so 0.1 + 0.2 = 0.30000000000000004. To get around this issue, you can multiply your numbers to remove the decimal portion. var hamburger = 8.20; var fries = 2.10; var total = hamburger + fries; console.log(total); //Outputs 10.299999999999999 hamburger = hamburger * 100; fries = fries * […]

JavaScript this

Posted on April 11th, 2013 by webninjataylor

You can scope this in JavaScript through call(), apply(), or bind().

RequireJS Basics

Posted on April 11th, 2013 by webninjataylor

RequireJS loads scripts asynchronously and out of order for speed Define modules with dependencies Written in JS, so “.js” is not needed when declaring scripts to load <script src=”/Scripts/require.js” data-main=”/Scripts/app/main.js”></script> Contents of main.js could look like this… require([ “mylibs/utils”, “mylibs/palette” ], function(utils, palette){ // The app is loaded… } ); Defining dependencies in a JS […]

Node.js Basics

Posted on April 11th, 2013 by webninjataylor

Node is written in C, not JavaScript Node is a server-side program which handles the grunt work of networking Node processes JavaScript which makes writing server-side code somewhat easier Node commands start with npm in the terminal

Backbone.js Basics

Posted on April 3rd, 2013 by webninjataylor

Overview Views observe Models The traditional controller role is performed by the template, not by the Backbone View Model persistence is the storage and synchronization of data A group of Models is a Collection A Model or Collection may have multiple Views observing it for changes Collections are useful for performing any aggregate computations across […]

When to Use the Backbone.js MVC (MV*) Framework

Posted on April 3rd, 2013 by webninjataylor

Good for building single-page applications with a complex user interface (heavy lifting for view rendering) and for reducing the number of HTTP requests (data manipulation).  Web applications such as GMail, LinkedIn, and Facebook.  Do you want the page/view rendering handled by the browser or server? Resources Addy Osmani, Developing Backbone.js Applications (O’Reilly, 2013)

JavaScript: Creating Your Own Global Namespace

Posted on March 18th, 2013 by webninjataylor

Start with a global object variable declaration and extend that object with properties… var wnt = {}; //Start with the global declaration wnt.util = {}; //Add a new level (if desired) wnt.util.urlparameter = function(name) { return decodeURI( (RegExp(name + ‘=’ + ‘(.+?)(&|$)’).exec(location.search)||[,null])[1] ); }; //Turn a level into a method alert(eval(wnt.util.urlparameter(‘page’))-1); //assumes URL ends with […]

Tags:
Posted in Web ShotsComments Off on JavaScript: Creating Your Own Global Namespace

JavaScript If/Else Shorthand

Posted on March 18th, 2013 by webninjataylor

Writing this… if (index === 2) { return null } else { return value; } Is the same as saying… //return [test condition] ? [return this if true] : [return this if false] return index === 2 ? null : value; OR var myVariable = condition ? this_if_true : that_if_false;

jQuery Overview

Posted on March 18th, 2013 by webninjataylor

A JavaScript library that helps you write less and do more. $() is the same as jquery() Selectors: $(#id), $(element), $(.class), $(#multiple .selectors), $(p:first), $(p:last), $(p)[0] Adding .eq(#) counts 0-based forward or backwards from beginning or end of a set .size() method returns number of elements in the set $(document).ready(function(){…}); is the same as using […]

Mask Emails with JavaScript

Posted on March 18th, 2013 by webninjataylor

Some simple JavaScript to mask emails from spammers… function grimauld12(ww, f, h){ var cmd1 = “ma”; var cmd2 = “ilt”; var cmd3 = “o:”; location.href = cmd1+cmd2+cmd3+ww+”@”+f+”.”+h; }