Encapsulate Your JavaScript: Keep Private Methods Private

Encapsulate Your JavaScript: Keep Private Methods Private

ne of the questions I hear asked most frequently is: Is JavaScript a true object-oriented language? There’s no easy answer for the question. It certainly helps beginning JavaScript programmers to think of writing JavaScript in terms of objects, but that doesn’t answer the question.



Problem: Is JavaScript a true, object-oriented language? If so, what OOP features does it support, and how can you use them?



Solution: JavaScript 1.2 supports some object-oriented principles such as encapsulation and prototype-based inheritance. You’ll see how to create objects with private properties and methods.Is JavaScript Object-Oriented?
When considering object-oriented programming languages, it’s convenient to exclude JavaScript from the set of modern, object-oriented languages. JavaScript does, however, make use of some object-oriented principles. In this article, I’ll show you how JavaScript handles inheritance and encapsulation. Because I have a Java background, and because Java is an object-oriented language, I’ll be making comparisons between JavaScript and Java throughout this article.

One reason JavaScript is not considered an object-oriented language is that, unlike Java, JavaScript is not strongly typed. JavaScript, variables do not possess data types (which has nothing to do with how hard it is to type the code!). Rather, a variable’s data type depends on the value you assign to a variable. Further, you are free to change the type by simply assigning a different type of data to the variable or treating the data in a different way. For example in JavaScript you can write:

   var myVar = “a string”;   myVar = 4;   alert(“myVar = ” + myVar);
The code initially creates the myVar variable as a string but it becomes an integer by assigning 4 to it on the second line. Then, when the script displays the value, it casts the integer variable myVar back into a string, this time containing a string representation of the integer value. This is perfectly legal in JavaScript. In fact, it’s one of the reasons why JavaScript is easier to write than Java?you don’t have to worry about what type of data you are dealing with. In Java, the compiler would raise an error for either of the preceding lines of code. The equivalent Java would be:

   String myString = “a string”;   int myInt = 4;   System.out.println(“myInt = ” + myInt.toString()); 
In Java you must declare the data type with the variable and, in this case, you’d need two variables?one for each data type. If you want to change the data type of a variable in Java, you would create a new variable of the correct data type and then cast the variable from one type to the other. But sometimes casting just isn’t possible.

Another reason JavaScript is easily dismissed as an OOP language is that it doesn’t use class-based inheritance. In Java, you define objects in a logical hierarchy with more abstract objects at the top of the hierarchy and more concrete objects at the bottom. This top-down design makes it easy to say something like: “A four-wheeled vehicle is an object and a car is a specific kind of four-wheel vehicle.” That way, cars inherit all of the things that come with four-wheel vehicles and also add additional information specific to cars. Class-based inheritance is a powerful mechanism when you consider that the hierarchy can have many levels, each of which is added to all objects further down. In essence, you design objects in Java by determining how they are different from other, existing objects.

   function personName() {     this.firstname=null;     this.lastname=null;   }
JavaScript doesn’t support class-based inheritance, but it does support prototype-based inheritance. Prototype-based inheritance differs from class-based inheritance in that properties and methods are inherited from a constructor only, not from a class higher up in the hierarchy. All object constructors in JavaScript create objects whose properties and methods are inherited from the properties and methods of the prototype.Building JavaScript Objects
Suppose you wanted to create an object in JavaScript modeled after a person. This person object will contain two properties (for now)?a last name and a first name. First, design the prototype function:

   function person() {      }
Next, add the parameters used to initialize the properties:

   function person(first,last){      }
Finally, assign the parameters to property placeholders within the prototype:

   function person(first,last) {      this.firstName = first;      this.lastName = last;   }
Notice that the properties, firstName and lastName, are nested within the prototype. The this keyword seems to associate them with the object but, as you’ll see a little later, the this keyword also makes the properties publicly available to outside routines. In fact, the nesting of the properties alone makes them local to the object.

Later in your code, you can create as many person objects as you’d like, each of which automatically has firstName and lastName properties:

   var tDuffy = new person(“Tom”,”Duffy”);
So, if JavaScript does indeed use inheritance, does it also make use of other object-oriented principles? The answer is?although it’s under-used, yes; JavaScript also handles encapsulation.Encapsulation
The concept of encapsulation means that an object’s data members should remain private to the object and manipulated through publicly exposed methods. In other words, programs should not have the ability to change the properties of objects directly. In traditional JavaScript, you are free to modify the values of an object’s properties simply through assignment. For example, you can modify a person object’s data fields directly, which breaks the concept of encapsulation:

   tDuffy.firstName = “Kelli”;
Encapsulation states that firstName should not be available directly for manipulation and should be manipulated through a public method:

   tDuffy.setFirstName(“Kelli”);
Most JavaScripters find the creation of private data members (variables and methods) foreign; however, it’s rather easy to implement. A typical Java class contains a constructor, private member variables, and methods, and so does a JavaScript object. You need two techniques to accomplish encapsulation in JavaScript: nested functions and omission of the this keyword in object constructors. Be aware, though, that nested functions are part of the JavaScript 1.2 release, so they won’t be available in downlevel browsers.Build the Example
Open your favorite text editor, enter or download the code in Listing 1, and save the file as myObject.js:

This code looks slightly different than most JavaScript. Notice that all the code exists within the function definition?in other words, the member functions are nested within the myObject() function. The myObject() function is the constructor for objects of type myObject. Nothing new there. The next two lines declare two member variables, prop1 and prop2, and initialize them. Notice that the function omits the this keyword and uses the var keyword instead. Omitting the this keyword makes the variables private to the object. The var keyword makes the property local (each object instance gets its own copy of the variable). Calls to internal methods then assign the passed argument values to the properties (see the SideBar Cross-Browser Issues with the Sample Code).

The next part of the code is a list of method pointers defined with the this keyword. The method pointers make the methods public. Each method pointer points to a getter or setter method that lets you retrieve and set the values of the private data member values. In essence, they let the object control how the values change internally. Although most of the methods shown only manipulate properties, you’re can include non-property methods. For example, the getProps() method returns the list of property getters and setters.. Note that any method included inside the constructor that does not possess a method pointer is a private method. A private method can be used only by the object itself?it cannot be called from outside the object.

After declaring the method pointers, the myObject() function defines a set of nested functions. Only instances of myObject will have access to these private functions. Consider the following:

   var myObj = new myObject(“Tom”,”Duffy”);   alert(getProp1());
The second line throws an error indicating that getprop1() is undefined. The correct call looks something like this:

   alert(myObj.getprop1());
That code will display “Tom”?as it should.

Finally, note that the private function called privateMethod() is invisible outside of the myObject function. By omitting the method pointer to privateMethod() and nesting it within the myObject() function, neither a calling script nor an object instance can make use of privateMethod(). It exists solely to provide some mechanism to another public method inside the object?in this case the trivial testPrivateMethod() function.A Test Run
Open a new document in your text editor, add the following code, and save it as oopTest.htm in the same folder as myObject.js:

            OOP JavaScript Test                  

Property 1:

Property 2:

Change Property 1 to:

Change Property 2 to:

Then open the file in a JavaScript 1.2 capable browser (I test with IE 5.0, Netscape 4.7, and Netscape 6.0). Start by entering values in the first two text fields and initialize the object. Then modify the values in the next two fields to change the properties. Finally, test the private method. You should see three alert boxes. The first two alerts display the values you entered. The third alert displays the predefined properties set in the private method.

As you can see, JavaScript can be more object-oriented than most people think. The ECMAScript specification, to which JavaScript must adhere, lists future reserved words such as class, super, import, and extends. These reserved words are all used by Java for class-based inheritance. This indicates to me that JavaScript will migrate even closer to Java in the not-too-distant future.

The chief benefit of using object-oriented principles is code reuse. In fact, the encapsulation process detailed above closely mirrors Java’s component architecture?the JavaBeans specification. By encapsulating the functionality of an object, you or any other developer can simply forget about the implementation and make use of the functionality in any future project.

devx-admin

devx-admin

Share the Post:
Poland Energy Future

Westinghouse Builds Polish Power Plant

Westinghouse Electric Company and Bechtel have come together to establish a formal partnership in order to design and construct Poland’s inaugural nuclear power plant at

EV Labor Market

EV Industry Hurting For Skilled Labor

The United Auto Workers strike has highlighted the anticipated change towards a future dominated by electric vehicles (EVs), a shift which numerous people think will

Soaring EV Quotas

Soaring EV Quotas Spark Battle Against Time

Automakers are still expected to meet stringent electric vehicle (EV) sales quotas, despite the delayed ban on new petrol and diesel cars. Starting January 2023,

Affordable Electric Revolution

Tesla Rivals Make Bold Moves

Tesla, a name synonymous with EVs, has consistently been at the forefront of the automotive industry’s electric revolution. The products that Elon Musk has developed

Poland Energy Future

Westinghouse Builds Polish Power Plant

Westinghouse Electric Company and Bechtel have come together to establish a formal partnership in order to design and construct Poland’s inaugural nuclear power plant at the Lubiatowo-Kopalino site in Pomerania.

EV Labor Market

EV Industry Hurting For Skilled Labor

The United Auto Workers strike has highlighted the anticipated change towards a future dominated by electric vehicles (EVs), a shift which numerous people think will result in job losses. However,

Soaring EV Quotas

Soaring EV Quotas Spark Battle Against Time

Automakers are still expected to meet stringent electric vehicle (EV) sales quotas, despite the delayed ban on new petrol and diesel cars. Starting January 2023, more than one-fifth of automobiles

Affordable Electric Revolution

Tesla Rivals Make Bold Moves

Tesla, a name synonymous with EVs, has consistently been at the forefront of the automotive industry’s electric revolution. The products that Elon Musk has developed are at the forefront because

Sunsets' Technique

Inside the Climate Battle: Make Sunsets’ Technique

On February 12, 2023, Luke Iseman and Andrew Song from the solar geoengineering firm Make Sunsets showcased their technique for injecting sulfur dioxide (SO₂) into the stratosphere as a means

AI Adherence Prediction

AI Algorithm Predicts Treatment Adherence

Swoop, a prominent consumer health data company, has unveiled a cutting-edge algorithm capable of predicting adherence to treatment in people with Multiple Sclerosis (MS) and other health conditions. Utilizing artificial

Personalized UX

Here’s Why You Need to Use JavaScript and Cookies

In today’s increasingly digital world, websites often rely on JavaScript and cookies to provide users with a more seamless and personalized browsing experience. These key components allow websites to display

Geoengineering Methods

Scientists Dimming the Sun: It’s a Good Thing

Scientists at the University of Bern have been exploring geoengineering methods that could potentially slow down the melting of the West Antarctic ice sheet by reducing sunlight exposure. Among these

why startups succeed

The Top Reasons Why Startups Succeed

Everyone hears the stories. Apple was started in a garage. Musk slept in a rented office space while he was creating PayPal with his brother. Facebook was coded by a

Bold Evolution

Intel’s Bold Comeback

Intel, a leading figure in the semiconductor industry, has underperformed in the stock market over the past five years, with shares dropping by 4% as opposed to the 176% return

Semiconductor market

Semiconductor Slump: Rebound on the Horizon

In recent years, the semiconductor sector has faced a slump due to decreasing PC and smartphone sales, especially in 2022 and 2023. Nonetheless, as 2024 approaches, the industry seems to

Elevated Content Deals

Elevate Your Content Creation with Amazing Deals

The latest Tech Deals cater to creators of different levels and budgets, featuring a variety of computer accessories and tools designed specifically for content creation. Enhance your technological setup with

Learn Web Security

An Easy Way to Learn Web Security

The Web Security Academy has recently introduced new educational courses designed to offer a comprehensible and straightforward journey through the intricate realm of web security. These carefully designed learning courses

Military Drones Revolution

Military Drones: New Mobile Command Centers

The Air Force Special Operations Command (AFSOC) is currently working on a pioneering project that aims to transform MQ-9 Reaper drones into mobile command centers to better manage smaller unmanned

Tech Partnership

US and Vietnam: The Next Tech Leaders?

The US and Vietnam have entered into a series of multi-billion-dollar business deals, marking a significant leap forward in their cooperation in vital sectors like artificial intelligence (AI), semiconductors, and

Huge Savings

Score Massive Savings on Portable Gaming

This week in tech bargains, a well-known firm has considerably reduced the price of its portable gaming device, cutting costs by as much as 20 percent, which matches the lowest

Cloudfare Protection

Unbreakable: Cloudflare One Data Protection Suite

Recently, Cloudflare introduced its One Data Protection Suite, an extensive collection of sophisticated security tools designed to protect data in various environments, including web, private, and SaaS applications. The suite

Drone Revolution

Cool Drone Tech Unveiled at London Event

At the DSEI defense event in London, Israeli defense firms exhibited cutting-edge drone technology featuring vertical-takeoff-and-landing (VTOL) abilities while launching two innovative systems that have already been acquired by clients.

2D Semiconductor Revolution

Disrupting Electronics with 2D Semiconductors

The rapid development in electronic devices has created an increasing demand for advanced semiconductors. While silicon has traditionally been the go-to material for such applications, it suffers from certain limitations.

Cisco Growth

Cisco Cuts Jobs To Optimize Growth

Tech giant Cisco Systems Inc. recently unveiled plans to reduce its workforce in two Californian cities, with the goal of optimizing the company’s cost structure. The company has decided to

FAA Authorization

FAA Approves Drone Deliveries

In a significant development for the US drone industry, drone delivery company Zipline has gained Federal Aviation Administration (FAA) authorization, permitting them to operate drones beyond the visual line of

Mortgage Rate Challenges

Prop-Tech Firms Face Mortgage Rate Challenges

The surge in mortgage rates and a subsequent decrease in home buying have presented challenges for prop-tech firms like Divvy Homes, a rent-to-own start-up company. With a previous valuation of

Lighthouse Updates

Microsoft 365 Lighthouse: Powerful Updates

Microsoft has introduced a new update to Microsoft 365 Lighthouse, which includes support for alerts and notifications. This update is designed to give Managed Service Providers (MSPs) increased control and

Website Lock

Mysterious Website Blockage Sparks Concern

Recently, visitors of a well-known resource website encountered a message blocking their access, resulting in disappointment and frustration among its users. While the reason for this limitation remains uncertain, specialists