The object-oriented approach to JavaScript series

Source: Internet
Author: User

Object-oriented language has a flag, that is, the concept of owning classes, abstract instance objects of public properties and methods, based on the class can create any number of instance objects, generally with encapsulation, inheritance, polymorphism characteristics! But the object in JS is different from the object in pure object-oriented language, the ECMA standard defines the object in JS: A collection of unordered attributes, whose properties can contain basic values, objects, or functions. The object that can be easily understood as JS is a set of unordered values in which the property or method has a name that can be accessed by the mapped name.

A re-understanding of object-oriented

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:

    • All things Are object
    • object has encapsulation and inheritance attributes
    • Use message communication between objects and objects, each with 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.

In fact, the JavaScript language implements object-oriented programming in a way called prototypes (prototype) . The following is a discussion of class-based (class-based) object -oriented and prototype-based (prototype-based) object-oriented The difference between the two approaches in constructing an objective world.

Object-oriented and prototype-based object-oriented approach comparison of two classes

In class-based object-oriented mode, objects (object) are generated by classes (class) . In the prototype-based object-oriented approach, the object is dependent on the constructor (constructor) to exploit The original (prototype) constructed. 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 drawing here is like the classin the language, and the car is in accordance with this class (class) Manufactured, on the other hand, workers and machines (equivalent to constructor) use various components such as engines, tires, and steering wheels (the equivalent of prototype's various properties) to construct the car.

In fact, there is a debate about who has expressed the object-oriented idea more thoroughly in both ways. But I think the prototype object-oriented is a more thorough object-oriented approach, for the following reasons:

First of all, the object in the objective world is the result of other physical object constructs, and the abstract "drawing" cannot produce "automobile", that is to say, the class is an abstract concept rather than an entity, and the object is produced by an entity;

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 have been constructed by way of prototype.

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 object's behavior, State belongs to the object itself, and can be inherited together, which is closer to the objective reality.

Finally, the class-oriented object language, such as Java, allows for the declaration of static and static methods in a class in order to compensate for the inconvenience of using global functions and variables in a procedural language. 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 runtime (runtime) context, which is reflected in the change of this pointer. It is this characteristic that is closer to the natural view of "All things belong, the universe is the root of all things". In program Listing 1, window is similar to the concept of the universe.

Three JavaScript Object-oriented features

Abstract: seize the key points of need (such as Employee payroll system, only need to know the name, work number can be, do not need height, weight and other attributes)

Package: TV is like a black box, the internal principle of the circuit board nothing, as long as the surface of the function will be good! For example, JQ, will be used on the line, the inside of the principle can be temporarily regardless.

inheritance: The father has some functions, and the son inherits these functions. The son can also have some new features and features according to his own needs. The goal is to reuse code for maximum purpose.

Four Understanding objects

First type: Object-based

var New  = ' My Name '=function()    {return this . Name;

Second: The object literal way (a more clear Lookup object contains the properties and methods)

var person = {    ' My name ',    +,    function() {         return  This . Name;}    }

JS objects can use the '. ' operator to dynamically extend its properties, you can use the ' delete ' operator or set the property value to ' undefined ' to delete the property. As follows:

Person.newatt= 'new Attr '; // Add attribute alert (Person.newatt); // New Attr Delete Person.age;alert (person.age); // undefined (the value is undefined after deleting the attribute);

The object-oriented approach to JavaScript series

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.