Browse DevX
Sign up for e-mail newsletters from DevX


Encapsulate Your JavaScript: Keep Private Methods Private-2 : Page 2




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date