04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 22: The Evolution of JavaScript<br />

The current ECMAScript 4 description provides no information about accessing superclass methods<br />

from a subclass.<br />

Final Classes and Methods<br />

You can prevent classes from being extended and methods from being overridden by specifying either as<br />

final . A final class can never be inherited from and any attempts to do this will result in an error;<br />

likewise, final methods cannot be overridden in a subclass or an error will occur, as in this example:<br />

class Person {<br />

var name: string;<br />

var age: int;<br />

}<br />

final function sayName(){<br />

alert(this.name);<br />

}<br />

class Employee extends Person {<br />

var job: string;<br />

}<br />

override function sayName(){<br />

alert(this.name + “:” + this.job);<br />

}<br />

//error!!!<br />

final class Friend extends Person {<br />

}<br />

In this example, the Person class method sayName() is specified as final so the Employee class cannot<br />

override it. The Friend class is defined as final, so it can never be inherited from.<br />

Namespaces<br />

ECMAScript 4 introduces the concept of namespaces. Namespaces are compile - time values that group<br />

functionality together. You can define a namespace by using the namespace directive as follows:<br />

namespace myNamespace;<br />

namespace xhtml = “http://www.w3.org/1999/xhtml/”;<br />

Once a namespace is created, you can annotate classes, types, and the like as being part of the<br />

namespace. Here’s an example:<br />

myNamespace type Point = { x: int, y: int };<br />

myNamespace class Box {<br />

//more code here<br />

}<br />

732

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!