GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Main Page
  • Help
  • Browse
  • Cookbook
  • Wikijunior
  • Featured books
  • Recent changes
  • Special pages
  • Random book
  • Using Wikibooks
  • Reading room forum
  • Community portal
  • Help out!
  • Policies and guidelines

This view

  • Original page
  • New address

GeezerVison is showing en.wikibooks.org — JavaScript/Inheritance - Wikibooks, open books for an open world

< JavaScript


This page or section is an undeveloped draft or outline.
You can help to develop the work, or you can ask for assistance in the project room.


instanceof operator

The instanceof operator determines whether an object was instantiated as a child of another object, returning true if this was the case. instanceof is a binary infix operator whose left operand is an object and whose right operand is an object type. It returns true, if the left operand is of the type specified by the right operand. It differs from the .constructor property in that it "walks up the prototype chain". If object a is of type b, and b is an extension of c, then a instanceof b and a instanceof c both return true, whereas a.constructor === b returns true, while a.constructor === c returns false.

Inheritance by prototypes

The prototype of an object can be used to create fields and methods for an object. This prototype can be used for inheritance by assigning a new instance of the superclass to the prototype.[1]

function CoinObject() {
 this.value = 0; 
 this.diameter = 1;
}

function Penny() {
 this.value = 1;
}
Penny.prototype = new CoinObject();

function Nickel() {
 this.value = 5;
}
Nickel.prototype = new CoinObject();

Inheritance by functions


function CoinObject() {
 this.value = 0; 
 this.diameter = 1;
}

References

  1. ↑ Matthew Batchelder. "Object Oriented JavaScript" describes one way to implement inheritance in JavaScript.



Retrieved from "https://en.wikibooks.org/w/index.php?title=JavaScript/Inheritance&oldid=4666278"
Category:
  • Book:JavaScript
Hidden categories:
  • Stubs
  • Wikibooks pages with to-do lists
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.