Getting Good with JavaScript by Andrew Burgess

Getting Good with JavaScript by Andrew Burgess

Author:Andrew Burgess
Language: eng
Format: mobi, epub, pdf
Publisher: Rockable Press


The first way of changing this that we'll look at is yet another keyword: new. But first, some context.

You know what a JavaScript object is. Keeping most of your related functionality in an appropriate object and accessing it via an appropriate interface is a very (very, very) basic description of what's called "Object-Oriented Programming" (OOP). Many programming languages use classes to do OOP. Classes are like blueprints: they aren't the real objects, but they lay out what the objects will have when they are created. JavaScript is very object-oriented, but it doesn't use classes. Instead, it uses prototypes. Prototypes are real objects (the same objects you've already seen), and we can use them as a "living" blueprint for other objects.

With all that said, let's get back to new. The classes in other languages use constructor functions to construct the new objects. In JavaScript, constructors are regular functions, written to create new object and called with the keyword new in front of them. Let's take a look at how to write and call this type of function.

Let's say we want to create truck objects. I know: this isn't necessarily practical. You probably won't ever need truck objects in JavaScript, but it's something you'll be familiar with while we discuss new and it's crew.

So, here's a function that will create a truck:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.