Learning from jQuery

Learning from jQuery

Callum Macrae

Language: English

Pages: 116

ISBN: 1449335195

Format: PDF / Kindle (mobi) / ePub

If you're comfortable with jQuery but a bit shaky with JavaScript, this concise guide will help you expand your knowledge of the language-especially the code that jQuery covers up for you. Many jQuery devotees write as little code as possible, but with some JavaScript under your belt, you can prevent errors, reduce overhead, and make your application code more efficient. This book explores event handling, prototypes, and working with the Dom and Ajax through examples and lots of code. You'll learn common conventions and patterns in JavaScript and-if you've never coded with JavaScript before-a tutorial will take you through the basics.

  • Enhance your jQuery code by using object constructors and prototypes
  • Reduce overhead and gain more control by handling events with JavaScript
  • Work with the Dom much faster with JavaScript than you can with jQuery
  • Send a few Ajax requests without having to load the entire jQuery library
  • Understand the importance of JavaScript code standards, comments, code reuse, and anti-patterns
  • Enlist JavaScript resources, such as a good IDE, a syntax checker, and version control

Dart: Up and Running


Exploring Cognitive Development: The Child As Problem Solver

Smashing Node.js

Build Awesome Command-Line Applications in Ruby: Control Your Computer, Simplify Your Life


















document.getElementsByTagName('p').getElementsByClassName('test'); // This works: var elements = document.getElementsByTagName('p'), allElements = []; Array.prototype.forEach.call(elements, function (element) { children = element.getElementsByClassName('test'); allElements = allElements.concat(Array.prototype.slice.call(children)); }); // This also works: document.querySelectorAll('p .test'); Selecting Children | 29 Selecting the Next Element To select the next sibling of an element in

argue that the day when micro-optimizations are no longer necessary is not yet here. However, it is important to know a few of the most effective micro-optimizations for JavaScript; if you’re writing code that could be used on the server side or on very old browsers, micro-optimizations could help. To read about optimizing your code in far more detail than I could fit into a section of a chapter, check out High Performance JavaScript by Nicholas C. Zakas. Algorithms The easiest and most

script. Caching Variables When looping through an array or NodeList, you generally call the .length property at every iteration. Every time you read that property, the length has to be calculated, which can be expensive. Instead, you can cache the length in a variable: for (var i = 0, l = ary.length; i < l; i++) { // Do something here } When finding an element using jQuery, you’d be wise to cache it instead of calling the jQuery function multiple times: $('#foo').click(function () {

of function expressions and function declarations. As a personal preference only, I have placed a space before the parentheses in the expressions but not the declaration. This is to mark them as function expressions, as it sometimes isn’t clear at first glance: (function test () { // Do something }()); test(); // Error; not defined I also put a semicolon after the function expressions, as dumb minifiers prefer it; you can put a function declaration on one line with stuff after it without a

are to use setInterval to check whether the function has changed a variable and run some code when it has, or use eval: Functions | 55 // Warning: antipattern! function call(func) { eval(func + '()'); } Obviously, that function is dangerous and vulnerable. A slightly better example can be explained using event handling. (The following code isn’t actually possible, but imagine that it is for this example.) onEventSetToTrue will set clicked to true when #foo is clicked: var clicked = false;

Download sample