Javascript object-oriented


JavaScript is an object-oriented programming language (OOP), but with the usual C + +, Java and other object-oriented languages ​​are different, such as JavaScript, there is no class (class) concept. So in accordance with the thinking of OOP to write JavaScript code, there will always be a little unnatural feeling.

In fact, JavaScript is based on the object (object-based) language, you can think that almost all things inside JavaScript are objects. In the previous tutorials and examples, although we almost did not mention the concept of the object, but after the completion of this chapter, you will know, in fact, they are based on the object.

What is an object?

An object is a collection of properties and methods. Here is a simple example to help understand what is an object and some concepts related to the object. For example, we are a person, is an object, then:

Properties and methods: people have names, there are height and weight and other characteristics that we call attributes of the object. People can speak, can walk, this ability we call the object method.

Private methods and public methods: a man after learning the programming language can write a program, after learning a foreign language can be a translator, individual objects have this ability we call private methods object. Conversely, the above words, walking and so everyone has the method, known as the object of public methods.

Packaging: The same two people learn a programming language, although you can write a program, but because it is two different objects, often in written procedures there is a difference. Some of the data object and code can be private, can not be outside access, called the package.

Inheritance: Inheritance is the concept of the class inside. Inheritance refers to the subclass by inheriting (extends) the parent class, in order to obtain the parent class attributes and methods (except for private properties and methods). This is like a tree, through the inheritance layer stacked into the final level of the program is clear and powerful. Because the JavaScript language has no class concept, it does not directly provide inheritance, but now there are a lot of people and some JavaScript framework, are trying to have the ability to inherit JavaScript.

Polymorphism: Polymorphism is the ability of different things with different manifestations. The polymorphism mechanism reduces the complexity of the code by allowing objects with different internal structures to share the same external interface. Unfortunately, as with inheritance, JavaScript does not support polymorphism, a very important concept in OOP. Of course, there are a lot of people and JavaScript framework to try to indirectly achieve the JavaScript polymorphism.

Given the length and purpose of this tutorial, some basic concepts about objects are briefly described here. Object-oriented programming is a specialized discipline, interested students can also read on the OOP specialized works.

JavaScript Object Summary

An object is a data structure or an array structure that contains various properties and methods of manipulating those data. JavaScript is a loose type of language, so you can dynamically add properties to the object. The object's data type in JavaScript is object.

Copyright 2014-2017 by All Rights Reserved.