JavaScript syntax: Difference between revisions

Content deleted Content added
Nelsonkam (talk | contribs)
Exception handling: Changed formatting.
Tags: Mobile edit Mobile web edit
Nelsonkam (talk | contribs)
Inheritance: Improved formatting for better rendering on mobile version.
Tags: Mobile edit Mobile web edit
Line 1,339:
JavaScript supports inheritance hierarchies through prototyping in the manner of [[Self (programming language)|Self]].
 
In the following example, the <tt>{{mono|Derived</tt>}} class inherits from the <tt>{{mono|Base</tt>}} class.
When <tt>{{mono|d</tt>}} is created as <tt>{{mono|Derived</tt>}}, the reference to the base instance of <tt>{{mono|Base</tt>}} is copied to <tt>{{mono|d.base</tt>}}.
 
Derive does not contain a value for <tt>{{mono|aBaseFunction</tt>}}, so it is retrieved from <tt>Base</tt>{{mono|aBaseFunction}} ''when <tt>{{mono|aBaseFunction</tt>}} is accessed''. This is made clear by changing the value of <tt>{{mono|base.aBaseFunction</tt>}}, which is reflected in the value of <tt>{{mono|d.aBaseFunction</tt>}}.
 
Some implementations allow the prototype to be accessed or set explicitly using the <tt>{{mono|__proto__</tt>}} slot as shown below.
 
<syntaxhighlight lang=JavaScript>