background preloader

Concept

Facebook Twitter

jQuery Coding Standards and Best Practices. Deferreds. At a high-level, deferreds can be thought of as a way to represent asynchronous operations which can take a long time to complete.

Deferreds

They're the asynchronous alternative to blocking functions and the general idea is that rather than your application blocking while it awaits some request to complete before returning a result, a deferred object can instead be returned immediately. You can then attach callbacks to the deferred object: they will be called once the request has actually completed. In its most basic form, a "promise" is a model that provides a solution for the concept of deferred (or future) results in software engineering. The main idea behind it is something we've already covered: rather than executing a call which may result in blocking, we instead return a promise for a future value that will eventually be satisfied. If it helps to have an example here, consider that you are building a web application which heavily relies on data from a third party API.

Code Organization Concepts. When you move beyond adding simple enhancements to your website with jQuery and start developing full-blown client-side applications, you need to consider how to organize your code.

Code Organization Concepts

In this chapter, we'll take a look at various code organization patterns you can use in your jQuery application and explore the RequireJS dependency management and build system. Before we jump into code organization patterns, it's important to understand some concepts that are common to all good code organization patterns. Your code should be divided into units of functionality — modules, services, etc. Avoid the temptation to have all of your code in one huge $( document ).ready() block. This concept, loosely, is known as encapsulation.Don't repeat yourself. The concept of loose coupling can be especially troublesome to developers making their first foray into complex applications, so be mindful of this as you're getting started. An object literal is perhaps the simplest way to encapsulate related code. Details of the object model - JavaScript.

The content of this article is under discussion.

Details of the object model - JavaScript

Please provide feedback and help us to make this page better: bug 1201380. JavaScript is an object-based language based on prototypes, rather than being class-based. Because of this different basis, it can be less apparent how JavaScript allows you to create hierarchies of objects and to have inheritance of properties and their values. This chapter attempts to clarify the situation. This chapter assumes that you are already somewhat familiar with JavaScript and that you have used JavaScript functions to create simple objects. Class-based vs. prototype-based languagesEdit. Les variables statiques en JavaScript. Les variables statiques sont assez utiles dans certains cas et alors que d’autres langages dont il est très proche le permettent nativement, le JavaScript demande de tricher un peu pour obtenir cette fonctionnalité… Revenons d’abord aux bases : une variable statique, c’est quoi et où est l’intérêt ?

Les variables statiques en JavaScript

Lorsque l’on déclare une variable dans une fonction, la portée de cette variable ne dépasse pas celle de la fonction : en-dehors de cette dernière, la variable n’existe pas. Ainsi, à chaque appel de la fonction, la variable est déclarée à nouveau et ne conserve pas la valeur qu’elle a pu obtenir lors d’un précédent appel. Et c’est là toute la magie des variables statiques : une telle variable conserve sa valeur tout au long de l’exécution du programme.

Voici ce que ça donne par exemple en C++ : Que se passe-t-il si on appelle cette fonction ? C’est un concept qui peut s’avérer utile dans pas mal de cas et, nativement, le JavaScript ne le gère pas. Simple JavaScript Inheritance. I’ve been doing a lot of work, lately, with JavaScript inheritance – namely for my work-in-progress JavaScript book – and in doing so have examined a number of different JavaScript classical-inheritance-simulating techniques.

Simple JavaScript Inheritance

Out of all the ones that I’ve looked at I think my favorites were the implementations employed by base2 and Prototype. I wanted to go about extracting the soul of these techniques into a simple, re-usable, form that could be easily understood and didn’t have any dependencies. Additionally I wanted the result to be simple and highly usable. Here’s an example of what you can do with it: A couple things to note about this implementation: Creating a constructor had to be simple (in this case simply providing an init method does the trick).In order to create a new ‘class’ you must extend (sub-class) an existing class.All of the ‘classes’ inherit from a single ancestor: Class.