Saturday, February 2, 2013

instanceof & typeof Operators

Sometimes, even if it is considered bad habit, we need to check the class of an object in JavaScript. Classes may be defined by the programmer, but I will cover this topic later, for now let's just focus on classes defined by the interpreter. There are many, for example:
  • Object
  • String
  • Number
  • Date
  • Function
  • ...
To check if an object is an instance of a specific class, JavaScript offers the instanceof method, which may be used in the following way:

var mynmbr = new Number(1);
var mystrng = new String("disasterjs!");
var myfunc = function(){
    console.log("hello world!");
mynmbr instanceof Number //returns true
mynmbr instanceof Object //returns true
mystrng instanceof String //returns true
mystrng instanceof Object //returns true
mynmbr instanceof String //returns false
myfunc instanceof Function //returns true

Be careful when using instanceof about what you are checking. For example numbers and strings not wrapped in the constructor are treated as primitive value, thus a call for instanceof in that case would produce an unexpected result:

var mystrng = "disasterjs!";
var mynmbr = 1;

mynmbr instanceof Number //returns false
mynmbr instanceof Object //returns false
mystrng instanceof String //returns false
mystrng instanceof Object //returns false

What about the typeof operator? This operator returns the class as a string. The following table summarises its possible return values:

Host object (provided by the JS environment)Implementation-dependent
E4X XML object"xml"
Any other object"object"

Programmer may be careful here when checking a Null object as the result of a typeof will return "object". Here are a couple of examples on how to use typeof.

typeof 1 === "number" //true
typeof Number(1) === "number" //true
typeof 1 === "object" //false
typeof undefined_variable === "undefined" //true
typeof true === "boolean" //true

And that's it.

1 comment:

  1. You will find 2 referrals which greatest clarify a brief history from the rolex replica. The foremost is, nicely, the very first the actual Ref. 6511 had been cased through Brevet within possibly rolex replica sale or even flower precious metal, this solely showcased the red-colored day steering wheel along with coordinating red-colored "DAY-DATE" textual content, as well as utilized possibly rolex replica sale or even leader fingers. The look had been finished with a jubilee band, including much more heft which very first assisted gather this interest. The actual motion, Quality 1055, had been driven through a computerized rotor, the actual very had been made from rolex replica sale polymer, and also the caseback had been strong. Rolex Ref. 6511 had been, even though magnificent, nicely detaileda rolex replica really worth a lot more than it's pounds within precious metal. The 2nd, and perhaps much more essential, research that needs to be pointed out may be the typically hublot replica uk which solidified the actual watch's location being an image Ref. 1803.