typeof and Instanceof in JavaScript

Source: Internet
Author: User


typeof is a unary operator, while Instanceof is a two-dollar operator;

typeof operation is a variable, and instanceof is preceded by a variable, followed by a type;

typeof returns a string, and Instanceof returns a Boolean value.


1, typeof ()


Http://www.cnblogs.com/jikey/archive/2010/05/05/1728337.html


TypeOf is a unary operation that can be any type before it is placed in an operand.
The return value is a string that describes the type of the operand. , typeof generally can only return the following results:number,boolean,string,function,object,undefined.


Example

<! doctype html public  "-//w3c//dtd html 4.01 transitional//en"   "HTTP// Www.w3.org/TR/html4/loose.dtd ">

Results

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/86/9C/wKiom1fFN0DAytq7AABAdtNGTTc316.png "title=" 005. PNG "alt=" Wkiom1ffn0daytq7aabadtngttc316.png "/>



typeof

Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof


the typeof operator Returns a String indicating the type of the unevaluated operand.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/86/9C/wKioL1fFP9fTtJFqAAAyPkwLkX0389.png "title=" 007. PNG "alt=" Wkiol1ffp9fttjfqaaaypkwlkx0389.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/86/9C/wKiom1fFQAHg1d1gAACEybsqp0g657.png "title=" 008. PNG "alt=" Wkiom1ffqahg1d1gaaceybsqp0g657.png "/>

Example

 Numberstypeof 37 ===  ' number ';typeof 3.14 ===  ' number '; typeof  = ==  ' number ';typeof math.ln2 ===  ' number ';typeof infinity ===  ' number '; typeof nan ===  ' number '; // despite being  "Not-a-number" Typeof Number ( 1)  ===  ' number ';  // but never use this form!// stringstypeof   ""  ===  ' string ';typeof  "Bla"  ===  ' string ';typeof  (typeof 1)  ===   ' string ';  // typeof always returns a stringtypeof string ("abc")   ===  ' string ';  // but never use this form!// booleanstypeof true  ===  ' Boolean ';typeof false ===  ' Boolean '; Typeof boolean (true)  ===  ' Boolean ';  // but never use this form!// symbolstypeof symbol ()  == =  ' symbol ' Typeof&nbsP Symbol (' foo ')  ===  ' symbol ' typeof symbol.iterator ===  ' symbol '// undefinedtypeof  undefined ===  ' undefined ';typeof declaredbutundefinedvariable ===  ' undefined '; typeof undeclaredvariable ===  ' undefined '; // objectstypeof {a:1} ===  ' Object ';// use array.isarray or object.prototype.tostring.call// to  differentiate regular objects from arraystypeof [1, 2, 4] ===  ' Object '; typeof new date ()  ===  ' object ';// the following is confusing.  don ' T use!typeof new boolean (True)  ===  ' object '; typeof new  Number (1)  ===  ' object ';  typeof new string ("abc")  ===  ' object ';//  Functionstypeof function () {} ===  ' function ';typeof class c {} ===  ' function ';typeof math.sin ===  ' function '; 
null
This stands since the beginning of javascripttypeof null = = = ' object ';


in the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects is 0. typeof  return value. (reference)

A Fix was proposed to ECMAScript (via an opt-in), but is rejected. It would has resulted in typeof null === ‘null‘ .





2, instanceof


instanceof

Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof


the instanceof operatortests whether an object have theprototype property of A constructor in its prototype chain.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/86/9C/wKiom1fFO7WxIihTAAAo22TeoZQ424.png "title=" 006. PNG "alt=" Wkiom1ffo7wxiihtaaao22teozq424.png "/>

theinstanceofoperator tests presence ofconstructor.prototypeinchobject' s prototype chain.

Defining Constructorsfunction C () {}function D () {}var o = new C (); alert (o instanceof c);/true, Because:Object.getProt Otypeof (o) = = = C.prototypealert (o instanceof D);//False, because D.prototype is nowhere in O ' s prototype Chainalert (o INS Tanceof object);//Truealert (C.prototype instanceof object);//Truec.prototype = {};var O2 = new C (); alert (O2 instanceof C );//truealert (o instanceof c);//False, because C.prototype is nowhere in O ' s prototype chain anymored.prototype = new C () ;//Use Inheritancevar O3 = new D (); alert (O3 instanceof D);//truealert (O3 instanceof C);//true






typeof and Instanceof in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.