Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Object-Oriented JavaScript

You're reading from   Object-Oriented JavaScript

Arrow left icon
Product type Paperback
Published in Jul 2008
Publisher
ISBN-13 9781847194145
Length 356 pages
Edition Edition
Languages
Arrow right icon
Toc

Table of Contents (18) Chapters Close

Object-Oriented JavaScript
Credits
About the Author
About the Reviewers
Preface
1. Introduction FREE CHAPTER 2. Primitive Data Types, Arrays, Loops, and Conditions 3. Functions 4. Objects 5. Prototype 6. Inheritance 7. The Browser Environment 8. Coding and Design Patterns Reserved Words Built-in Functions
Built-in Objects Regular Expressions
Index

Array


The Array constructor creates array objects:

>>> var a = new Array(1,2,3);

This is the same as the array literal:

>>> var a = [1,2,3]; //recommended

When you pass only one numeric value to the Array constructor, it's assumed to be the array length. An array with this length will be created, and filled with undefined elements.

>>> var a = new Array(3);
>>> a.length

3

>>> a

[undefined, undefined, undefined]

This can sometimes lead to some unexpected behavior. For example, the following use of the array literal is valid:

>>> var a = [3.14]
>>> a

[3.14]

However, passing the floating-point number to the Array constructor is an error:

>>> var a = new Array(3.14)

invalid array length

Members of the Array Objects

Property/Method

Description

length

The number of elements in the array.

>>> [1,2,3,4].length

4

concat(i1, i2, i3,...)

Merges arrays together.

>>> [1,2].concat([10,20], [300,400])

[1, 2, 10, 20, 300, 400]

join(separator)

Turns an array into a string. The separator parameter is a string and the default value is a comma.

>>> [1,2,3].join()

"1,2,3"

>>> [1,2,3].join('|')

"1|2|3"

>>> [1,2,3].join(' is less than ')

"1 is less than 2 is less than 3"

pop()

Removes the last element of the array and returns it.

>>> var a = ['une', 'deux', 'trois'];
>>> a.pop()

"trois"

>>> a

["une", "deux"]

push(i1, i2,i3,...)

Appends elements to the end of the array and returns the length of the modified array.

>>> var a = [];
>>> a.push('zig', 'zag', 'zebra','zoo');

4

reverse()

Reverses the elements of the array and returns the modified array.

>>> var a = [1,2,3];
>>> a.reverse()

[3, 2, 1]

>>> a

[3, 2, 1]

shift()

Like pop() but removes the first element, not the last.

>>> var a = [1,2,3];
>>> a.shift();

1

>>> a

[2, 3]

slice

(start_index,

end_index)

Extracts a piece of the array without modifying the source array.

>>> var a = ['apple', 'banana','js', 'css', 'orange'];
>>> a.slice(2,4)

["js", "css"]

>>> a

["apple", "banana", "js", "css", "orange"]

sort(callback)

Sorts an array. Optionally accepts a callback function for custom sorting. The callback function receives two array elements as arguments and should return 0 if they are equal, 1 if the first is greater and -1 if the second is greater.

An example of a custom sorting function that does a proper numeric sort (since the default is character sorting):

function customSort(a, b){
  if (a > b) return 1; 
  if (a < b) return -1; 
  return 0;
}

Example use of sort():

>>> var a = [101, 99, 1, 5];
>>> a.sort();

[1, 101, 5, 99]

>>> a.sort(customSort);

[1, 5, 99, 101]

>>> [7,6,5,9].sort(customSort);

[5, 6, 7, 9]

splice(start, delete_count, i1, i2, i3,...)

This is probably the most powerful of the array functions. It can remove and add elements at the same time. The first parameter is where to start removing, the second is how many items to remove and the rest of the parameters are new elements to be inserted in the place of the removed ones.

>>> var a = ['apple', 'banana','js', 'css', 'orange'];
>>> a.splice(2, 2, 'pear', 'pineapple');

["js", "css"]

>>> a

["apple", "banana", "pear", "pineapple", "orange"]

unshift(i1, i2, i3,...)

Like push() but adds the elements at the beginning of the array as opposed to the end. Like shift() but adds to the array as opposed to removing from it. Returns the length of the modified array.

>>> var a = [1,2,3]; 
>>> a.unshift('one', 'two'); 

5

>>> a 

["one", "two", 1, 2, 3]

lock icon The rest of the chapter is locked
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Object-Oriented JavaScript
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Modal Close icon
Modal Close icon