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 />

ECMAScript 3.1 also adds two new properties to each function: name and parameters . The name<br />

property contains the name of the function as it appears when using a function declaration, and the<br />

parameters property is an array of strings containing the named function properties. Here is an<br />

example:<br />

function add(num1, num2){<br />

return num1 + num2;<br />

}<br />

alert(add.name); //”add”<br />

alert(add.parameters[0]);<br />

alert(add.parameters[1]);<br />

//”num1”<br />

//”num2”<br />

These properties help to inspect functions for debugging purposes. Note that if the function is<br />

anonymous, its name property will be an empty string.<br />

The last change to functions is with the arguments object. In ECMAScript 3, the arguments object was<br />

array - like, having numeric indices and a length property, but it was not actually an instance of Array .<br />

ECMAScript 3.1 makes the arguments object officially into an instance of Array , allowing usage of all<br />

array methods for processing arguments.<br />

Changes to Other Types<br />

There are some additional changes involving other data types in ECMAScript 3.1. One of the major<br />

issues in ECMAScript 3 was not being able to determine if an object was an array. The big problem was<br />

with arrays being passed from one frame to another. Since each frame has its own global object, each also<br />

had its own Array constructor, so an array from one frame wasn ’ t necessarily an instance of Array in<br />

another frame — clearly, this led to confusion.<br />

ECMAScript 3.1 introduces the Array.isArray() method to determine if an object is, in fact, an array.<br />

This method accepts a single argument, which is the object to check, and returns true if the object is<br />

determined to be an array. This works regardless of the object ’ s origin and is designed specifically to<br />

work around the cross - frame array identification problem. The method is used as follows:<br />

var nums = [1,2,3,4];<br />

var data = {};<br />

alert(Array.isArray(nums));<br />

alert(Array.isArray(data));<br />

//true<br />

//false<br />

In this code, Array.isArray() is used to determine which of the two objects is an array.<br />

Note that ECMAScript 3.1 also formalizes the JavaScript 1.6 array extras and JavaScript 1.8 array<br />

reductions discussed earlier in this chapter.<br />

Strings also get a new method, trim() , which removes white space from the beginning and end of text<br />

and returns the result. Here’s an example:<br />

var message = “ Hello world! “;<br />

var newMessage = message.trim();<br />

alert(newMessage); //”Hello world!”<br />

749

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

Saved successfully!

Ooh no, something went wrong!