type recognition
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
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;
};
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);
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;
};
var extendWithName = function (object, name) {
if (typeof object == "object") {
object.name = name;
}
};
extendWithName(null, "null");
var a = function () {};
extendWithName(a, "function");
WHAT IS YOUR TYPE?
ARE YOU AN OBJECT?
ARE YOU A NUMBER?
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;
};
THANK YOU