Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Architecting JavaScript Code

916 views

Published on

Published in: Spiritual
  • Be the first to comment

Architecting JavaScript Code

  1. 1. function myNonClosure() { Variable lost after var date = new Date(); function returns return date.getMilliseconds();}
  2. 2. function myClosure() { Variable stays aroundvar date = new Date(); even after function returns//nested functionreturn function () { return date.getMilliseconds(); };}
  3. 3. var calc = new Calculator(eqCtl);calc.add(2,2)
  4. 4. var Calculator = function() { //private variables //private functions return { //public members };};
  5. 5. var Calculator = function(eq) { //private member var eqCtl = document.getElementById(eq); return { var calculator = new Calculator(eq); //expose public member calculator.add(2,2) add: function(x,y) { var val = x + y; eqCtl.innerHTML = val; } };};
  6. 6. <html>... <script source="first.js"></script> <script source="second.js"></script></html>// first.js(function(ns) { // second.js ns.Customer = function() { (function(ns) { this.name = ""; ns.Order = function() { ... }; }; }(window.WilderMinds = window.WilderMinds || {}));}(window.WilderMinds = window.WilderMinds || {}));
  7. 7. <!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"> <title>RequireJS</title></head><body> <script src="scripts/require.js" data-main="scripts/lib/main.js"> </script></body></html>
  8. 8. // main.jsrequire(["Customer"], // Requires the Customer Module function (Customer) { // Call with required Module(s) // Your Initialization Code var c = new Customer("A Customer"); var name = c.name; } );
  9. 9. // Customer.jsdefine( [], // Required Scripts (None) function(){ // Gets any required modules here like main function Customer (name) { this.name = name } return Customer; // Return the object that Requires //constructor to allow you to call it}
  10. 10. Suresh.Balla@neudesic.com

×