KEMBAR78
JavaScript APIs In Focus | ODP
JavaScript APIs in Focus (with a short rant on quality of APIs in public libraries  and a direction to what and why should be made better) Amsterdam JS meeting 7th September 2010 Sergey Ilinsky ( @ilinsky )
1. What is wrong with my code? (bad library story) jQuery: >  $(document).each() TypeError: b is undefined >  $(document).click("alert(1)") TypeError: d is undefined >  $(document).map(null) TypeError: a is null Dojo: >  dojo.stopEvent() TypeError: evt is undefined >  dojo.ready([new Function])  TypeError: mll[x] is not a function >  dojo.map(null) TypeError: arr is null
1. What is wrong with my code? (good library story) An other library (Ample SDK): >  ample.documentElement.removeChild() Missing required 1st argument "node"  in "removeChild" function call >  ample.querySelector("xul|textbox[type=number]", 11); Incompatible type of 2nd argument "NSResolver"  in "querySelector" function call. Expecting "Function" >  ample.importNode(null) null is not allowed value of 1st argument "node"  in "importNode" function call
2. Why is it important to validate library API call?
2. Why is it important to validate library API call? 1) Helps library user (end developer) to find problems more efficiently
2. Why is it important to validate library API call? 1) Helps library user (end developer) to find problems more efficiently 2) Prevents library from internal failure caused by improper call
3. Three aspects of API call validation 1) Arguments requiredness 2) Arguments' Datatypes
3) null values
4. Guard.js — API validation library Guard.js  is a “broker library” that helps you  validating API calls to your “public library».  4.1) Using Guard.js 4.2) Guard exceptions in try/catch statements 4.3) Data types support 4.4) Validation execution overhead
4.1 Using Guard.js // Your «public library» code myObject.myFunction = function(sName, fHandler, bDirection, oArguments) { // Validate API function call Guard(arguments, [ // 'name' is required, must be string ["name",  String], // 'handler' is required, must be function ["handler",  Function], // 'direction' is optional, if passed must be boolean or null ["direction", Boolean, true, true], // 'arguments' is optional, if passed must be JS arguments object ["arguments", Guard.Arguments, true] ]); // Execute your library function here return "the output of myLibraryFunction"; };
4.2 Guard exceptions in try/catch statements function testMyObjectFunction() { try { myObjectFunction(3); } catch (e) { alert(e.caller) // ->  testMyObjectFunction } } testMyObjectFunction();
4.3 Data types support - Primitive Types - String - Number - Boolean - Object - «Object» Types - Array - Function - Date - RegExp - Special Types - Guard.Arguments - Custom Types - Any constructor function reference
10% 4.4 Validation execution overhead
5. Signing JavaScript Objects if (!window.XMLHttpRequest) { window.XMLHttpRequest = function() { return new ActiveXObject(“Microsoft.XMLHTTP”); } } 1) Signing Object window.XMLHttpRequest.prototype.toString = function() { return “[object XMLHttpRequest]”; } alert(new XMLHttpRequest) // [object XMLHttpRequest] 2) Signing Function window.XMLHttpRequest.toString = function() { return “function XMLHttpRequest() {\n\t[my library code]\n}”; } alert(XMLHttpRequest) // function XMLHttpRequest() { [my library code] }

JavaScript APIs In Focus

  • 1.
    JavaScript APIs inFocus (with a short rant on quality of APIs in public libraries and a direction to what and why should be made better) Amsterdam JS meeting 7th September 2010 Sergey Ilinsky ( @ilinsky )
  • 2.
    1. What iswrong with my code? (bad library story) jQuery: > $(document).each() TypeError: b is undefined > $(document).click("alert(1)") TypeError: d is undefined > $(document).map(null) TypeError: a is null Dojo: > dojo.stopEvent() TypeError: evt is undefined > dojo.ready([new Function]) TypeError: mll[x] is not a function > dojo.map(null) TypeError: arr is null
  • 3.
    1. What iswrong with my code? (good library story) An other library (Ample SDK): > ample.documentElement.removeChild() Missing required 1st argument "node" in "removeChild" function call > ample.querySelector("xul|textbox[type=number]", 11); Incompatible type of 2nd argument "NSResolver" in "querySelector" function call. Expecting "Function" > ample.importNode(null) null is not allowed value of 1st argument "node" in "importNode" function call
  • 4.
    2. Why isit important to validate library API call?
  • 5.
    2. Why isit important to validate library API call? 1) Helps library user (end developer) to find problems more efficiently
  • 6.
    2. Why isit important to validate library API call? 1) Helps library user (end developer) to find problems more efficiently 2) Prevents library from internal failure caused by improper call
  • 7.
    3. Three aspectsof API call validation 1) Arguments requiredness 2) Arguments' Datatypes
  • 8.
  • 9.
    4. Guard.js —API validation library Guard.js is a “broker library” that helps you validating API calls to your “public library». 4.1) Using Guard.js 4.2) Guard exceptions in try/catch statements 4.3) Data types support 4.4) Validation execution overhead
  • 10.
    4.1 Using Guard.js// Your «public library» code myObject.myFunction = function(sName, fHandler, bDirection, oArguments) { // Validate API function call Guard(arguments, [ // 'name' is required, must be string ["name", String], // 'handler' is required, must be function ["handler", Function], // 'direction' is optional, if passed must be boolean or null ["direction", Boolean, true, true], // 'arguments' is optional, if passed must be JS arguments object ["arguments", Guard.Arguments, true] ]); // Execute your library function here return "the output of myLibraryFunction"; };
  • 11.
    4.2 Guard exceptionsin try/catch statements function testMyObjectFunction() { try { myObjectFunction(3); } catch (e) { alert(e.caller) // -> testMyObjectFunction } } testMyObjectFunction();
  • 12.
    4.3 Data typessupport - Primitive Types - String - Number - Boolean - Object - «Object» Types - Array - Function - Date - RegExp - Special Types - Guard.Arguments - Custom Types - Any constructor function reference
  • 13.
    10% 4.4 Validationexecution overhead
  • 14.
    5. Signing JavaScriptObjects if (!window.XMLHttpRequest) { window.XMLHttpRequest = function() { return new ActiveXObject(“Microsoft.XMLHTTP”); } } 1) Signing Object window.XMLHttpRequest.prototype.toString = function() { return “[object XMLHttpRequest]”; } alert(new XMLHttpRequest) // [object XMLHttpRequest] 2) Signing Function window.XMLHttpRequest.toString = function() { return “function XMLHttpRequest() {\n\t[my library code]\n}”; } alert(XMLHttpRequest) // function XMLHttpRequest() { [my library code] }
  • 15.
    Guard.js Web-Site: http://github.com/ilinsky/guard.js Ample SDK Web-Site: http://www.amplesdk.com
  • 16.
  • 17.
  • 18.