📎 Webclip
Preparing Yourself for Modern JavaScript Development
The post argues that developers who know JavaScript basics can prepare for newer libraries by first understanding scope, modules, namespaces, and constructor functions. It presents these ideas as practical building blocks before moving on to tools like Backbone.js or Ember.js.
Reading notes#
- Code that places variables and functions directly in the global namespace creates pollution and raises the chance of collisions with other scripts.
- An IIFE creates scope by wrapping code in a function that is called immediately, so variables and functions stay out of the global namespace.
- Passing
window,jQuery, andundefinedinto an IIFE gives the code safer local references and avoids relying on possibly changed global values. - A namespace can be created with an object on
window, usingwindow.myApp = window.myApp || {}so the same object is reused across files. - A module can combine with a namespace by taking the namespace object as an argument and returning it after adding members.
- The revealing module pattern keeps variables private inside the module and returns an object that exposes only selected functions and values.
- JavaScript constructors use function calls with
newto create objects, and public instance data is assigned withthisinside the constructor. - Methods placed on a constructor’s
prototypeare shared by all instances instead of being duplicated on every object. - A
Spyconstructor can inherit fromPersonby setting its prototype tonew Person(), which lets instances use inherited methods such asfullName(). - After splitting code into modules, the next step is to look into combination and minification, with Rails asset pipeline, SquishIt, and ASP.NET MVC 4 mentioned as examples.
