typeof
can only return the base type like, object, strirng, number, undefined.
typeof o === "object"
instanceof
can be used if you are inheriting from the base class. Eg here MDN
function Base() {
this.name = "abc";
}
var child = new Base();
var a = child instanceof Base; //true
instance of expects the format <object> insanceof <function>
You can use isPrototypeOf()
when inheriting using Object.create()
var base = {name; "abc"};
var child = Object.create(base);
base.isPrototypeOf(child);
More info can be read here: Mozilla Developer Network: isPrototypeOf
To check if there exists an object o
with the attribute name
non empty, you can do
if(typeof o === "object" && typeof o.name !== "undefined")
A shorthand can be used, if name
will not hold falsy
values like 0
if(o && o.name)