An object-oriented understanding of JavaScript

Source: Internet
Author: User

To illustrate that JavaScript is a thoroughly object-oriented language, it is necessary to start with object-oriented concepts and explore some of the concepts in object-oriented:
A, all things are objects
B. Object has encapsulation and inheritance characteristics
C. Use message communication between objects and objects, each of which has information hiding
Based on these three points, C + + is a semi-object-oriented semi-oriented process language because, although he implements encapsulation, inheritance, and polymorphism of classes, there are global functions and variables of non-object nature. Java, C # is a complete object-oriented language that organizes functions and variables in the form of classes so that they cannot be separated from the object's existence. But here the function itself is a process, just attached to a class.
However, object-oriented is just a concept or a programming idea, and it should not depend on the existence of a language. For example, Java uses object-oriented thinking to construct its language, which implements such mechanisms as class, inheritance, derivation, polymorphism, and interface. But these mechanisms are just a means of implementing object-oriented programming, not a necessity. In other words, a language can choose the right way to implement object-oriented according to its own characteristics. So, because most programmers first learn or use a similar Java, C + + and other advanced compiler language (Java Although semi-compiled semi-interpretation, but generally as a compilation to explain), so the preconceived acceptance of the "class" this object-oriented implementation, in learning the scripting language, It is customary to use the concept of a class-oriented object language to determine whether the language is an object-oriented language or whether it has object-oriented characteristics. This is one of the important reasons that hinder programmers from learning and mastering JavaScript.

JavaScript language is the implementation of object-oriented programming in a way called prototypes (prototype). The following is a discussion of the differences in the way in which class-based (class-based) object-oriented and prototype-based (prototype-based) objects are constructed in an objective world.

class-based object-oriented and prototype-based object-oriented approach comparison,In class-based object-oriented mode, objects (object) are generated by the classes (class). In the prototype-based object-oriented approach, objects (object) are constructed using the constructor (constructor) with the prototype (prototype) .。 Give an example of an objective world to illustrate the differences in cognition in two ways. For example, a factory build a car, on the one hand, workers must refer to a drawing, design rules how the car should be manufactured. The engineering drawings here are like the classes in the language, and the cars are made out of this class, and on the other hand, workers and machines (equivalent to constructor) utilize various components such as engines, tires, and steering wheels (equivalent to prototype's various properties) to The car was constructed out.
In fact, there is a debate about who has expressed the object-oriented idea more thoroughly in both ways. But it is considered that the prototype object-oriented is a more thorough object-oriented approach, for the following reasons:
(1), first, the objective world in the production of objects is the result of other physical objects, and abstract "drawings" can not produce "car", that is, the class is an abstract concept rather than an entity, and the production of an object is an entity;
(2), second, according to the most basic object-oriented law of all things, the class itself is not an object, but the constructors (constructor) and prototypes (prototype) in the prototype form are themselves objects that other objects are constructed by way of prototype.
(3), again, in a class-oriented object language, the state of an object is held by an object instance (instance), and the object's behavior method is held by the class that declares the object, and only the structure and methods of the object can be inherited, whereas in the prototype object-oriented language, The behavior and state of objects belong to the object itself, and can be inherited together (reference resources), which is also closer to the objective reality.
(4), finally, the class-oriented object language such as Java, in order to make up for the inability to use the global functions and variables in the process-oriented language, to allow the declaration of static properties and static methods in the class. In fact, the objective world does not exist the so-called static concept, because all things are objects! In the prototype object-oriented language, the existence of global objects, methods or properties is not allowed, and there is no static concept except for the built-in objects (Build-in object).
All language elements (primitive) must depend on the existence of an object. However, due to the characteristic of functional language, the object that the language element relies on is changed with the context of runtime (runtime), which is reflected in the change of this pointer. It is this characteristic that is closer to the nature of "All things belong, the universe is the root of all things"

An object-oriented understanding of JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.