Type Recognition

3,211 views
3,086 views

Published on

15 min talk about importance of correct type recognition in methods of public APIs.

Not sure does it make any sense without narration, but anyway due to high demand, here it goes.

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

No Downloads
Views
Total views
3,211
On SlideShare
0
From Embeds
0
Number of Embeds
134
Actions
Shares
0
Downloads
40
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Type Recognition

  1. 1. TYPE RECOGNITION
  2. 2. var html = function (value) { if (typeof value == "string") { element.innerHTML = value; } if (typeof value == "function") { element.innerHTML = value(); } if (value == null) { return element.innerHTML; } return element; };
  3. 3. var a = { value: "I am an object", toString: function () { return this.value; } }; var b = new String("I am a string"); alert(a); el.html(a); alert(b); el.html(b);
  4. 4. var html = function (value) { if (typeof value == "function") { element.innerHTML = value(); } else if (value == null) { return element.innerHTML; } else { element.innerHTML = String(value); } return element; };
  5. 5. var extendWithName = function (object, name) { if (typeof object == "object") { object.name = name; } };
  6. 6. extendWithName(null, "null"); var a = function () {}; extendWithName(a, "function");
  7. 7. WHAT IS YOUR TYPE?
  8. 8. ARE YOU AN OBJECT?
  9. 9. ARE YOU A NUMBER?
  10. 10. var is = function (o, type) { type = (type + "").toLowerCase(); return (type == "object" && o === Object(o)) || (type == "undefined" && typeof o == type) || (type == "null" && o == null) || Object.prototype.toString.call(o) .slice(8, -1).toLowerCase() == type; };
  11. 11. THANK YOU

×