Download - Type Recognition

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


Top Related