ArticleDirectory
- Javascript methods that are easy to mix: typeof and instanceof
Javascript methods that are easy to mix: typeof and instanceof
September 28, 2011 by Ryan | 1 commentviews: 94 today views: 1
I am often confused by typeof and instanceof in Javascript, so I read it carefully. I hope you will not forget it any more.
Typeof ()
1
2
3
4
5
6
7
8
// Typeof () returns 6 data types: Number, String, Boolean, and Object
// Function and undefined.
VaR
A =
"String"
;
Typeof
()
// Outpout "string"
VaR
Fn =
Function
(){};
// Output "function"
Typeof
(FN)
// Output "function"
Instanceof ()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Instanceof () returns a Boolean value. Used to detect objects of one of the six basic types
// Is the instance generated by an object (constructor. Used to trace the prototype chain.
// An example is found online.
VaR
A =
Function
(){};
VaR
B =
Function
(){};
B. Prototype =
New
A;
VaR
C =
New
B;
VaR
D =
New
A;
Alert (C
Instanceof
A );
// True
Alert (d
Instanceof
A );
// True
Alert (
Instanceof
Function );
// True
Alert ({}
Instanceof
Function );
// False
A. Prototype = {};
// Modify the prototype chain
Alert (C
Instanceof
A );
// False
Alert (d
Instanceof
A );
// False
Categories: javascript | tags: instanceof, prototype, typeof | permalink
Address: http://www.coolicer.com/2011/09/28/javascript-typeof-and-instanceof.html