<ul>JavaScript APIs in Focus (with a short rant on quality of APIs in public libraries  and a direction to what and why sh...
1. What is wrong with my code? (bad library story) jQuery: >  $(document).each() TypeError: b is undefined >  $(document)....
1. What is wrong with my code? (good library story) An other library (Ample SDK): >  ample.documentElement.removeChild() M...
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 efficient...
3. Three aspects of API call validation 1) Arguments requiredness <ul><li>2) Arguments' Datatypes
3) null values </li></ul>
4. Guard.js — API validation library Guard.js  is a “broker library” that helps you  validating API calls to your “public ...
4.1 Using Guard.js // Your «public library» code myObject.myFunction = function(sName, fHandler, bDirection, oArguments) {...
4.2 Guard exceptions in try/catch statements function testMyObjectFunction() { try { myObjectFunction(3); } catch (e) { al...
4.3 Data types support - Primitive Types - String - Number - Boolean - Object - «Object» Types - Array - Function - Date -...
10% 4.4 Validation execution overhead
<ul>5. Signing JavaScript Objects </ul>if (!window.XMLHttpRequest) { window.XMLHttpRequest = function() { return new Activ...
Upcoming SlideShare
Loading in...5
×

JavaScript APIs In Focus

1,503

Published on

Slides from AmsterdamJS Meetup, September'10

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,503
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

JavaScript APIs In Focus

  1. 1. <ul>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) </ul><ul>Amsterdam JS meeting 7th September 2010 </ul>Sergey Ilinsky ( @ilinsky )
  2. 2. 1. What is wrong with my code? (bad library story) jQuery: > $(document).each() TypeError: b is undefined > $(document).click(&quot;alert(1)&quot;) 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. 3. 1. What is wrong with my code? (good library story) An other library (Ample SDK): > ample.documentElement.removeChild() Missing required 1st argument &quot;node&quot; in &quot;removeChild&quot; function call > ample.querySelector(&quot;xul|textbox[type=number]&quot;, 11); Incompatible type of 2nd argument &quot;NSResolver&quot; in &quot;querySelector&quot; function call. Expecting &quot;Function&quot; > ample.importNode(null) null is not allowed value of 1st argument &quot;node&quot; in &quot;importNode&quot; function call
  4. 4. 2. Why is it important to validate library API call?
  5. 5. 2. Why is it important to validate library API call? 1) Helps library user (end developer) to find problems more efficiently
  6. 6. 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
  7. 7. 3. Three aspects of API call validation 1) Arguments requiredness <ul><li>2) Arguments' Datatypes
  8. 8. 3) null values </li></ul>
  9. 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. 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 [&quot;name&quot;, String], // 'handler' is required, must be function [&quot;handler&quot;, Function], // 'direction' is optional, if passed must be boolean or null [&quot;direction&quot;, Boolean, true, true], // 'arguments' is optional, if passed must be JS arguments object [&quot;arguments&quot;, Guard.Arguments, true] ]); // Execute your library function here return &quot;the output of myLibraryFunction&quot;; };
  11. 11. 4.2 Guard exceptions in try/catch statements function testMyObjectFunction() { try { myObjectFunction(3); } catch (e) { alert(e.caller) // -> testMyObjectFunction } } testMyObjectFunction();
  12. 12. 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
  13. 13. 10% 4.4 Validation execution overhead
  14. 14. <ul>5. Signing JavaScript Objects </ul>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() { [my library code] }”; } alert(XMLHttpRequest) // function XMLHttpRequest() { [my library code] }
  15. 15. <ul>Guard.js </ul>Web-Site: http://github.com/ilinsky/guard.js <ul>Ample SDK <li>Web-Site: http://www.amplesdk.com
  16. 16. Sergey Ilinsky
  17. 17. http://www.ilinsky.com
  18. 18. http://twitter.com/ilinsky </li></ul><ul>6. Links </ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×