04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 22: The Evolution of JavaScript<br />

There are four built - in namespaces, as follows:<br />

❑<br />

❑<br />

❑<br />

❑<br />

__ES4__<br />

intrinsic<br />

iterator<br />

meta<br />

The __ES4__ namespace is the global namespace for ECMAScript 4 and is always in use. The<br />

instrinsic namespace is used to define internal properties or methods that should not be accessible in<br />

normal scripts. You will mostly likely never use or reference the instrinsic namespace. The iterator<br />

namespace is used specifically for methods and classes related to iterators. You ’ ve already seen the use<br />

of the meta namespace in defining catchall getters and setters on dynamic classes; it ’ s used for system<br />

protocols and cannot be extended by developers.<br />

You can use a particular namespace by default via the use namespace directive, as shown here:<br />

namespace version1_0;<br />

namespace version1_5;<br />

version1_0 function log(msg: String){<br />

alert(msg);<br />

}<br />

version1_5 function log(msg: String){<br />

console.log(msg);<br />

}<br />

use namespace version1_0;<br />

//calls version1_0 log();<br />

log(“Hello world!”);<br />

//calls version 1_5 log();<br />

version1_5::log(“Hello world!”);<br />

This code defines two namespaces: version1_0 and version 1_5 . Each namespace has a function<br />

defined called log() . In the default namespace, there is no definition for log() , so you must either use<br />

a namespace via use namespace to set it as the default or use the namespace as a qualifier followed by<br />

:: and then the function name.<br />

As with everything in ECMAScript, namespaces are also objects.<br />

733

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

Saved successfully!

Ooh no, something went wrong!