type recognition

11
TYPE RECOGNITION

Upload: dmitry-baranovskiy

Post on 15-May-2015

2.708 views

Category:

Education


2 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Type Recognition

TYPE RECOGNITION

Page 2: Type Recognition

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;

};

Page 3: Type Recognition

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);

Page 4: Type Recognition

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;

};

Page 5: Type Recognition

var extendWithName = function (object, name) {

if (typeof object == "object") {

object.name = name;

}

};

Page 6: Type Recognition

extendWithName(null, "null");

var a = function () {};

extendWithName(a, "function");

Page 7: Type Recognition

WHAT IS YOUR TYPE?

Page 8: Type Recognition

ARE YOU AN OBJECT?

Page 9: Type Recognition

ARE YOU A NUMBER?

Page 10: Type Recognition

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;

};

Page 11: Type Recognition

THANK YOU