Name It…It’s Yours

clone

In my previous post, I spoke about JavaScript objects. An object is simply a named collection of properties and methods. It can contain primitive data types, such as numbers, text and booleans. It can also contain complex data types, such as arrays, functions and even other objects.

So why mess with all of this object junk? Why not put everything in the global scope so you can access anything you want at any time? The answer is encapsulation. This is an object-oriented principle which speaks to the protection and organization of your data. If everything is in the global scope, everything is open for changing in your program and not logically organized. So you want as little global as possible. So objects give our data and program pieces more meaning and context.

JavaScript has its own built-in, standard objects, but you can also create your own custom objects. This is easily accomplished using constructors. A constructor is like a blueprint for an object. Its job is to establish properties and methods of an object. It can even set initial values of its properties. Here is an example…

function Invitation(who. what. when, where) {
	this.who = who;
	this.what = what;
	this.when = when;
	this.where = where;
}

Notice the “this” keyword. The “this” keyword allows us to refer to the context of properties. Basically, it means that the value of a property belongs to “this” constructor. For example, this.who is a property of “this” Invitation constructor.

Another thing to note is the capital “I”. In JavaScript, you typically begin with lowercase, but when it comes to constructors, you use uppercase. This is not strictly enforced, but is a standard convention.

So how do constructors help us? As I stated, they are blueprints to instantiate other objects. So we copy objects from objects. Here is an example using our Invitation constructor…

// Store the new object in a variable (invitation)
var invitation = new Invitation("Joe Schmoe", "Birthday Party", "December 12, 2017", "Jane's House");

We now can access a new instance of the Invitation object with all of the new values we passed to it.

Happy Coding!

Clay Hess

More To Explore

Code

Exploring the Broadcast Channel API: Inter-Tab Communication

Intercommunication between different contexts (like tabs, iframes or workers) of the same origin has often been a challenge. With the Broadcast Channel API, developers now have a powerful tool to easily communicate between browsing contexts. In this blog post, we’ll dive deep into the capabilities of the Broadcast Channel API, exploring its features, use cases, and how it can be effectively implemented in your projects.

computer, laptop, work place-2982270.jpg
Code

Unlocking Wireless Communication: A Dive into the Bluetooth API

Wireless communication has become an integral part of our daily lives, and Bluetooth technology is at the forefront of this revolution, enabling devices to exchange data over short distances and creating a world more interconnected than ever before. At the heart of this technology lies the Bluetooth Application Programming Interface (API), a powerful tool for developers looking to harness the capabilities of Bluetooth in their applications. In this blog post, we’ll explore what the Bluetooth API is, how it works, and the possibilities it opens up for innovation in wireless communication.

Share This Post

small_c_popup.png

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.