Your SlideShare is downloading. ×
JavaScript APIs In Focus
JavaScript APIs In Focus
JavaScript APIs In Focus
JavaScript APIs In Focus
JavaScript APIs In Focus
JavaScript APIs In Focus
JavaScript APIs In Focus
JavaScript APIs In Focus
JavaScript APIs In Focus
JavaScript APIs In Focus
JavaScript APIs In Focus
JavaScript APIs In Focus
JavaScript APIs In Focus
JavaScript APIs In Focus
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

JavaScript APIs In Focus

1,482

Published on

Slides from AmsterdamJS Meetup, September'10

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,482
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1.
      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 )
  • 2. 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
  • 3. 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
  • 4. 2. Why is it important to validate library API call?
  • 5. 2. Why is it important to validate library API call? 1) Helps library user (end developer) to find problems more efficiently
  • 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. 3. Three aspects of API call validation 1) Arguments requiredness
    • 2) Arguments' Datatypes
    • 8. 3) null values
  • 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 exceptions in try/catch statements function testMyObjectFunction() { try { myObjectFunction(3); } catch (e) { alert(e.caller) // -> testMyObjectFunction } } testMyObjectFunction();
  • 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. 10% 4.4 Validation execution overhead
  • 14.
      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() { [my library code] }”; } 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. Sergey Ilinsky
    • 17. http://www.ilinsky.com
    • 18. http://twitter.com/ilinsky
      6. Links

×