Book Image

Object-Oriented JavaScript - Second Edition - Second Edition

Book Image

Object-Oriented JavaScript - Second Edition - Second Edition

Overview of this book

JavaScript is the behavior, the third pillar in today's paradigm that looks at web pages as something that consists of clearly distinguishable parts: content (HTML), presentation (CSS) and behavior (JavaScript). Using JavaScript, you can create not only web pages but also desktop widgets, browser and application extensions, and other pieces of software. It's a pretty good deal: you learn one language and then code all kinds of different applications. While there's one chapter specifically dedicated to the web browser environment including DOM, Events and AJAX tutorials, the rest is applicable to the other environments Many web developers have tried coding or adopting some bits of JavaScript, but it is time to "man up" and learn the language properly because it is the language of the browser and is, virtually, everywhere. This book starts from zero, not assuming any prior JavaScript programming knowledge and takes you through all the in-depth and exciting futures hidden behind the facade. Once listed in the "nice to have" sections of job postings, these days the knowledge of JavaScript is a deciding factor when it comes to hiring web developers. After reading this book you'll be prepared to ace your JavaScript job interview and even impress with some bits that the interviewer maybe didn't know. You should read this book if you want to be able to take your JavaScript skills to a new level of sophistication.
Table of Contents (19 chapters)
Object-Oriented JavaScript Second Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Built-in Functions
Regular Expressions
Index

Appendix B. Built-in Functions

This appendix contains a list of the built-in functions (methods of the global object), discussed in Chapter 3, Functions.

Function

Description

parseInt()

Takes two parameters: an input object and radix; then tries to return an integer representation of the input. Doesn't handle exponents in the input. The default radix is 10 (a decimal number). Returns NaN on failure. Omitting the radix may lead to unexpected results (for example for inputs such as 08), so it's best to always specify it.

> parseInt('10e+3');
10
> parseInt('FF');
NaN
> parseInt('FF', 16);
255

parseFloat()

Takes a parameter and tries to return a floating-point number representation of it. Understands exponents in the input.

> parseFloat('10e+3');
10000
> parseFloat('123.456test');
123.456

isNaN()

Abbreviated from "Is Not a Number". Accepts a parameter and returns true if the parameter is not a valid number, false otherwise. Attempts to convert the input to a number first.

> isNaN(NaN);
true
> isNaN(123);
false
> isNaN(parseInt('FF'));
true
> isNaN(parseInt('FF', 16));
false

isFinite()

Returns true if the input is a number (or can be converted to a number), but is not the number Infinity or -Infinity. Returns false for infinity or non-numeric values.

> isFinite(1e+1000);
false
> isFinite(-Infinity);
false
> isFinite("123");
true

encodeURIComponent()

Converts the input into a URL-encoded string. For more details on how URL encoding works, refer to the Wikipedia article at http://en.wikipedia.org/wiki/Url_encode.

> encodeURIComponent('http://phpied.com/');
"http%3A%2F%2Fphpied.com%2F"
> encodeURIComponent('some script?key=v@lue');
"some%20script%3Fkey%3Dv%40lue"

decodeURIComponent()

Takes a URL-encoded string and decodes it.

> decodeURIComponent('%20%40%20');
" @ "

encodeURI()

URL-encodes the input, but assumes a full URL is given, so returns a valid URL by not encoding the protocol (for example, http://) and hostname (for example, www.phpied.com).

> encodeURI('http://phpied.com/');
"http://phpied.com/"
> encodeURI('some script?key=v@lue');
"some%20script?key=v@lue"

decodeURI()

Opposite of encodeURI().

> decodeURI("some%20script?key=v@lue");
"some script?key=v@lue"

eval()

Accepts a string of JavaScript code and executes it. Returns the result of the last expression in the input string.

To be avoided where possible.

> eval('1 + 2');
3
> eval('parseInt("123")');
123
> eval('new Array(1, 2, 3)');
[1, 2, 3]
> eval('new Array(1, 2, 3); 1 + 2;');
3