1

I am struggling with the keyword 'instance'. In Java, I only know how to create the "new" keyword. Now, I'm learning about JavaScript, some author heavily used the word 'instance':

function Foo(){}

var foo = new Foo();

//foo is now an instance of Foo
console.log(foo instanceof Foo ) //=> true

What is the purpose of making foo instanceof Foo?

Ashish
  • 1,943
  • 2
  • 14
  • 17
user3057928
  • 605
  • 1
  • 9
  • 12
  • Please dont mix Javascript and Java. They are vastly different. You want Javascript – Johnny Harlamert Dec 06 '13 at 05:55
  • Read about "OOP inheritance" in general, and "prototypes" in JavaScript, you'll get the idea. You just need a bit more research... – elclanrs Dec 06 '13 at 05:56
  • what is the purpose of making foo instanceof Foo? – user3057928 Dec 06 '13 at 05:57
  • @user3057928, the command (foo instanceof Foo) is a type-checker that returns true or false. It's there so you can determine if foo *is* an instance of the class Foo. If it isn't an instance of that class, it will return "false". For instance, `var notFoo = 1; notFoo instanceof Foo;` will return false, since notFoo is not an instance of Foo. – jamesmortensen Dec 06 '13 at 06:08
  • Maybe the following will help: http://stackoverflow.com/a/16063711/1641941 An instance is an object that could have been created by a constructor function (this case Foo) Foo has a prototype and all members that cannot be found on the instances will be looked up on Foo's prototype so prototype declared members are shared. – HMR Dec 06 '13 at 07:20
  • @user3057928 if your concept about instance and instance of is cleared by my answer you can upvote and select my answer – Ashish Jan 29 '14 at 10:22

5 Answers5

1

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

For more JavaDoc.

But this is in Java. Not Javascript

Community
  • 1
  • 1
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

instanceof is a binary operator, The instanceof allows to check if the object is created by given constructor:

The instanceof operator returns true if object is an instance of class. It returns true if true if class is present in the object's prototype chain. It returns false if object is not an instance of class, or if object is null.

function Rabbit() { }
var rabbit = new Rabbit

alert(rabbit instanceof Rabbit) // true

In the example above, the match is found at the first step, because: rabbit.proto == Rabbit.prototype.

Few helpful links

What is the instanceof operator in JavaScript?

http://www.w3schools.com/js/js_objects.asp

http://javascript.info/tutorial/instanceof#the-instanceof-operator

Community
  • 1
  • 1
Satheesh Cheveri
  • 3,621
  • 3
  • 27
  • 46
1

You have a class Person. By a class I mean you define what are the attributes that a Person should have eg:name,age,weight etc.. Now you can say, I am creating an "instanceof" Person with name "Peter" , age =25 and weight ="70 pounds". Now. when you call "is instanceof" the properties of the object which you have are mapped against the the attributes/signature/metadata of the class to which you are comparing..

Eg:

class Person
{
    String name;
    int age;
    double weight;
}

....

Person p = new Person();
boolean b = (p isInstanceOf Person) // returns true
Ashish
  • 1,943
  • 2
  • 14
  • 17
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

instanceof

JavaScript doesn't have classes (yet), but it does have constructor functions, an instanceof operator that defines a relationship between objects and constructors, and a form of inheritance based on prototype chains.

instance

A variable will hold a reference to an instance of an object.

The actual object is an instance.

Sridhar R
  • 20,190
  • 6
  • 38
  • 35
0

instanceof is a keyword.

It checks if an object reference is an instance of a type, and returns a boolean value;

The <object-reference> instanceof Object will return true for all non-null object references, since all Java objects are inherited from Object. instanceof will always return false if <object-reference> is null.

or simply we can say that:

The instanceof operator provides us with a way to test which constructor function was used to create a given object.It evaluates as true if the constructor function of the specific object referenced was the one used to create the object being tested and evaluates as false if the constructor function of the specified object was not the one used.

Ashish
  • 1,943
  • 2
  • 14
  • 17