Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

1. JavaScript Objects > Verify that an Object Is an Instance of a Particular Co...

Verify that an Object Is an Instance of a Particular Constructor Function

By using the instanceof operator, we can determine (true or false) if an object is an instance of a particular constructor function.

Below, we are verifying if the object InstanceOfCustomObject is an instance of the CustomConstructor constructor function. This works with user-defined objects as well as native objects created with the new operator.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

// user-defined object constructor
var CustomConstructor = function() {this.foo = 'bar';};

// instantiate an instance of CustomConstructor
var instanceOfCustomObject = new CustomConstructor();

console.log(instanceOfCustomObject instanceof CustomConstructor); // logs true

// works the same as a native object
console.log(new Array('foo') instanceof Array) // logs true

</script></body></html>

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint