Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

JavaScript/Inheritance

From Wikibooks, open books for an open world
<JavaScript
Constructors and prototypesJavaScript
Inheritance
Access control


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


instanceof operator

[edit |edit source]

Theinstanceof operator determines whether an object was instantiated as a child of another object, returningtrue 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 returnstrue, 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 objecta is of typeb, andb is an extension ofc, thena instanceof b anda instanceof c both returntrue, whereasa.constructor === b returnstrue, whilea.constructor === c returnsfalse.

Inheritance by prototypes

[edit |edit source]

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]

functionCoinObject(){this.value=0;this.diameter=1;}functionPenny(){this.value=1;}Penny.prototype=newCoinObject();functionNickel(){this.value=5;}Nickel.prototype=newCoinObject();

Inheritance by functions

[edit |edit source]
Clipboard

To do:
Provide a recommended method for function-based inheritance - one reference ishttp://www.crockford.com/javascript/inheritance.html


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

References

[edit |edit source]
  1. Matthew Batchelder."Object Oriented JavaScript" describes one way to implement inheritance in JavaScript.


Constructors and prototypesJavaScript
Inheritance
Access control


Retrieved from "https://en.wikibooks.org/w/index.php?title=JavaScript/Inheritance&oldid=4193615"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp