JavaScript instanceof Learning

Source: Internet
Author: User

In JS, we generally judge what type of variable is often used with the typeof operator, but you find that no matter what type of object is referenced, it returns the type of object. In fact, sometimes we would like to see what this object is specific type, so in JS introduced the instanceof operator. The instanceof operator is similar to the typeof operator and is used to view the type.

Let's start with the syntax of typeof and Instanceof    :

For typeof: typeof option

option is an object that needs to be instrumented, or it can be an expression that returns the type of option in the form of a string.

For Instanceof:option Instanceof Constructor

option is the object that needs to be detected, constructor is the object you are sure of, and returns the Boolean value of whether this option belongs to the constructor type. See a simple example:

functionStudent () {}varStudenta =NewStudent (); Console.info (StudentainstanceofStudent)//trueConsole.info (typeofStudenta)//' object 'Console.info (typeof{k:1})//' object 'Console.info (typeof300)//' number 'Console.info (typeof' Hello World ')//' String 'Console.info (typeofa!= ' Undefind ')//true

Did you find it? Although instanceof may not be as good as typeof, then it is easy to think about it, and you can use TypeOf to determine whether a variable exists without the If judgment, but instanceof must know the type of comparison (this is constructor). But when it comes to typeof an object (that is, when the return value is Object), the benefits of instanceof are still great. For example, in the inheritance can clearly see the advantages:

function Person () {}   ==instanceof Student)   //trueinstanceof person)    //

This article mainly talk about instanceof, for TypeOf will not be too much to explain. In general , we use instanceof to determine whether an instance is of a certain type. For example the above:

instanceof Student   // determine if Studenta belongs to Student type

This is what the book says: instanceof  operators can be used to judge a constructor'sprototype属性是否存在另外一个要检测对象的原型链上。也就是说用来判断constructor.prototype是否存在于参数option的原型链上(对于JS里面的原型链,我这里就不讲解)。再看一个例子:

functionMy () {}functionYou () {}varMyone =NewMy (); Console.info (MyoneinstanceofMy)//trueConsole.info (MyoneinstanceofYou)//falseMy.prototype= {};//modified the My.prototypevarMytwo =NewMy (); Console.info (MytwoinstanceofMy)//trueConsole.info (MyoneinstanceofMy)//False,my.prototype points to an emptyYou.prototype=NewMy ();//modified the Your.prototypevarMythree =NewYou (); Console.info (MythreeinstanceofYou)//trueConsole.info (MythreeinstanceofMy)//true

When in this case of inheritance or to determine the type of the instance, it is better to use instanceof than typeof.

JavaScript instanceof Learning

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.