Book Image

Object-Oriented JavaScript - Third Edition

By : Ved Antani, Stoyan STEFANOV
5 (1)
Book Image

Object-Oriented JavaScript - Third Edition

5 (1)
By: Ved Antani, Stoyan STEFANOV

Overview of this book

JavaScript is an object-oriented programming language that is used for website development. Web pages developed today currently follow a paradigm that has three clearly distinguishable parts: content (HTML), presentation (CSS), and behavior (JavaScript). JavaScript is one important pillar in this paradigm, and is responsible for the running of the web pages. This book will take your JavaScript skills to a new level of sophistication and get you prepared for your journey through professional web development. Updated for ES6, this book covers everything you will need to unleash the power of object-oriented programming in JavaScript while building professional web applications. The book begins with the basics of object-oriented programming in JavaScript and then gradually progresses to cover functions, objects, and prototypes, and how these concepts can be used to make your programs cleaner, more maintainable, faster, and compatible with other programs/libraries. By the end of the book, you will have learned how to incorporate object-oriented programming in your web development workflow to build professional JavaScript applications.
Table of Contents (25 chapters)
Object-Oriented JavaScript - Third Edition
About the Authors
About the Reviewer
Customer Feedback
Built-in Functions
Regular Expressions

A bit of history

Initially, the web was not much more than just a number of scientific publications in the form of static HTML documents connected together with hyperlinks. Believe it or not, there was a time when there was no way to put an image in a page. However, that soon changed. As the web grew in popularity and size, the webmasters who were creating HTML pages felt they needed something more. They wanted to create richer user interactions, mainly driven by the desire to save server round trips for simple tasks such as form validation. Two options came up-Java applets and LiveScript, a language conceived by Brendan Eich at Netscape in 1995 and later included in the Netscape 2.0 browser under the name of JavaScript.

The applets didn't quite catch on, but JavaScript did. The ability to use short code snippets embedded in HTML documents and alter otherwise static elements of a web page was embraced by the webmaster community. Soon, the competing browser vendor, Microsoft, shipped Internet Explorer (IE) 3.0 with JScript, which was a reverse engineered version of JavaScript plus some IE-specific features. Eventually, there was an effort to standardize the various implementations of the language, and this is how ECMAScript was born. European Computer Manufacturers Association (ECMA) created the standard called ECMA-262, which describes the core parts of the JavaScript programming language without browser and web page-specific features.

You can think of JavaScript as a term that encompasses the following three pieces:

  • ECMAScript: The core language-variables, functions, loops, and so on. This part is independent of the browser and this language can be used in many other environments.

  • Document Object Model (DOM): This provides ways to work with HTML and XML documents. Initially, JavaScript provided limited access to what's scriptable on the page, mainly forms, links, and images. Later, it was expanded to make all elements scriptable. This led to the creation of the DOM standard by the World Wide Web Consortium (W3C) as a language-independent (no longer tied to JavaScript) way to manipulate structured documents.

  • Browser Object Model (BOM): This is a set of objects related to the browser environment and was never part of any standard until HTML5 started standardizing some of the common objects that exist across browsers.

While there is one chapter in this book dedicated to the browser, the DOM, and the BOM, most of this book describes the core language and teaches you skills you can use in any environment where JavaScript programs run.

Browser wars and renaissance

For better or for worse, JavaScript's instant popularity happened during the period of the browser wars I (approximately 1996 to 2001). Those were the times during the initial Internet boom when the two major browser vendors, Netscape and Microsoft, were competing for market share. Both were constantly adding more bells and whistles to their browsers and their versions of JavaScript, DOM, and BOM, which naturally led to many inconsistencies. While adding more features, the browser vendors were falling behind on providing proper development and debugging tools and adequate documentation. Often, development was a pain; you would write a script while testing in one browser, and once you're done with development, you test in the other browser, only to find that your script simply fails for no apparent reason, and the best you can get is a cryptic error message, such as operation aborted.

Inconsistent implementations, missing documentation, and no appropriate tools painted JavaScript in such a light that many programmers simply refused to bother with it.

On the other hand, developers who did try to experiment with JavaScript got a little carried away, adding too many special effects to their pages without much regard of how usable the end results were. Developers were eager to make use of every new possibility the browsers provided, and ended up enhancing their web pages with things such as animations in the status bar, flashing colors, blinking texts, objects stalking your mouse cursor, and many other innovations that actually hurt the user experience. These various ways to abuse JavaScript are now mostly gone, but they were one of the reasons why the language had something of a bad reputation. Many serious programmers dismissed JavaScript as nothing but a toy for designers to play around with, and dismissed it as a language unsuitable for serious applications. The JavaScript backlash caused some web projects to completely ban any client-side programming and trust only their predictable and tightly controlled server. And really, why would you double the time to deliver a finished product and then spend additional time debugging problems with the different browsers?

Everything changed in the years following the end of the browser wars I. A number of events reshaped the web development landscape in a positive way. Some of them are given as follows:

  • Microsoft won the war with the introduction of IE6, the best browser at the time, and for many years they stopped developing Internet Explorer. This allowed time for other browsers to catch up and even surpass IE's capabilities.

  • The movement for web standards was embraced by developers and browser vendors alike. Naturally, developers didn't like having to code everything two (or more) times to account for browsers' differences; therefore, they liked the idea of having agreed-upon standards that everyone would follow.

  • Developers and technologies matured and more people started caring about things such as usability, progressive enhancement techniques, and accessibility. Tools such as Firebug made developers much more productive and the development less of a pain.

In this healthier environment, developers started finding out new and better ways to use the instruments that were already available. After the public release of applications such as Gmail and Google Maps, which were rich on client-side programming, it became clear that JavaScript is a mature, unique in certain ways, and powerful prototypal object-oriented language. The best example of its rediscovery was the wide adoption of the functionality provided by the XMLHttpRequest object, which was once an IE-only innovation, but was then implemented by most other browsers. XMLHttpRequest object allows JavaScript to make HTTP requests and get fresh content from the server in order to update some parts of a page without a full page reload. Due to the wide use of the XMLHttpRequest object, a new breed of desktop-like web applications, dubbed Ajax applications, was born.

The present

An interesting thing about JavaScript is that it always runs inside a host environment. The web browser is just one of the available hosts. JavaScript can also run on the server, on the desktop, and on mobile devices. Today, you can use JavaScript to do all of the following:

  • Create rich and powerful web applications (the kind of applications that run inside the web browser). Additions to HTML5, such as application cache, client-side storage, and databases, make browser programming more and more powerful for both online and offline applications. Powerful additions to Chrome WebKit also include support for service workers and browser push notifications.

  • Write server-side code using Node.js, as well as code that can run using Rhino (a JavaScript engine written in Java).

  • Make mobile applications; you can create apps for iPhone, Android, and other phones and tablets entirely in JavaScript using PhoneGap or Titanium. Additionally, apps for Firefox OS for mobile phones are entirely in JavaScript, HTML, and CSS. React Native from Facebook is an exciting new way to develop native iOS, Android, and Windows (experimental) applications using JavaScript.

  • Create rich media applications, such as Flash or Flex, using ActionScript, which is based on ECMAScript.

  • Write command-line tools and scripts that automate administrative tasks on your desktop using Windows Scripting Host (WSH) or WebKit's JavaScriptCore, which is available on all Macs.

  • Write extensions and plugins for a plethora of desktop applications, such as Dreamweaver, Photoshop, and most other browsers.

  • Create cross-operating system desktop applications using Mozilla's XULRunner and Electron. Electron is used to build some of the most popular apps on the desktop, such as Slack, Atom, and Visual Studio Code.

  • Emscripten, on the other hand, allows code written in C/C++ to be compiled into an asm.js format, which can then be run inside a browser.

  • Testing frameworks like PhantomJS are programmed using JavaScript.

  • This is by no means an exhaustive list. JavaScript started inside web pages, but today it's safe to say it is practically everywhere. In addition, browser vendors now use speed as a competitive advantage and are racing to create the fastest JavaScript engines, which is great for both users and developers, and opens doors for even more powerful uses of JavaScript in new areas such as image, audio and video processing, and games development.

The future

We can only speculate what the future will be, but it's quite certain that it will include JavaScript. For quite some time, JavaScript may have been underestimated and underused (or maybe overused in the wrong ways), but every day, we witness new applications of the language in much more interesting and creative ways. It all started with simple one-liners, often embedded in HTML tag attributes, such as onclick. Nowadays, developers ship sophisticated, well-designed and architected, and extensible applications and libraries, often supporting multiple platforms with a single codebase. JavaScript is indeed taken seriously, and developers are starting to rediscover and enjoy its unique features more and more.

Once listed in the nice-to-have sections of job postings, today, knowledge of JavaScript is often a deciding factor when it comes to hiring web developers. Common job interview questions you can hear today include-Is JavaScript an object-oriented language? Good. Now, how do you implement inheritance in JavaScript? After reading this book, you'll be prepared to ace your JavaScript job interview and even impress your interviewers with some bits that, maybe, they didn't know.