Object-oriented javascript---dynamic type language

Source: Internet
Author: User

Object-oriented javascript---Dynamic type language dynamic type language and interface-oriented programming

JavaScript does not provide class inheritance in the traditional object-oriented language, but instead implements the inheritance between objects and objects by means of a prototype delegate.
JavaScript also does not provide support for abstract classes and interfaces at the language level.

Because of these inconsistencies with the traditional object-oriented language, we need to differentiate from the traditional object-oriented language when writing code in design mode. It is necessary to understand some JavaScript in terms of object-oriented knowledge first.

The programming language can be divided into two types by data type, one is static type language and the other is dynamic type language.

The static type language determines the type of the variable at compile time, and the variable type of the dynamic type language will have some type when the variable is given a value when it is run by the program.

The advantages of a static type language:
? 1. At compile time, we can find the type mismatch error, the editor can help us avoid some errors that may occur during the program running.
? 2. If the data type is explicitly specified in the program, the compiler can also perform some optimizations on the program to improve the program execution speed.
Disadvantages of statically typed languages:
? 1. Forcing programmers to write programs in accordance with a strong contract, to prescribe data types for each variable, boils down to a means to help us write high-reliability programs, rather than to write programs, after all, most people write programs to fulfill demand delivery.
? 2. Types of declarations also add more code, and in the process of programming, these details distract the programmer's energy from thinking about the business logic.

Advantages of dynamic Type languages:
? The number of code written is less, it looks more concise, and the programmer can focus more on the business logic. While not distinguishing between types can make programs difficult to understand in some cases, the less code you have in general, the more you focus on logical expressions, the more useful it is for reading programs.
Disadvantages of dynamic type languages:
? It is not possible to guarantee the type of the variable, which may result in a type-related error during the program's run time. It's like buying a bag of spicy beef in a shop, but it's not beef until you actually eat it in your mouth.

In JavaScript, when we assign a value to a variable, we obviously don't need to consider its type, so JavaScript is a typical dynamic type language. The tolerance of dynamic type language to variable type gives a lot of flexibility to the actual coding. Because there is no need for type detection, we can try to invoke any method of any object without having to consider whether it was originally designed to own the method.

This is all based on the concept of duck type (duck typing), the popular saying is: "If it walks like a duck, it is also called duck, then it is a duck." ”

Get a deeper understanding of the type with a small story:

Once in the JavaScript kingdom, there was a king who felt that the most beautiful voice in the world was the cry of a duck, and the king called the minister to form a choir of 1000 ducks. The ministers searched all over the country and finally found 999 ducks, but there was still one, and the last minister found a very special chicken, which was the same as the duck, so the chicken became the last member of the choir.

The story tells us that the king wants to listen to the ducks, whether the owner of the sound is a chicken or a duck is not important. Duck type instructs us to focus only on the behavior of the object, not on the object itself.

Use the code to simulate the story:

var duck = {    duckSinging: function(){        console.log( ‘嘎嘎嘎‘ );    }};var chicken = {    duckSinging: function(){        console.log( ‘嘎嘎嘎‘ );    }};var choir = []; // 合唱团var joinChoir = function( animal ){    if ( animal && typeof animal.duckSinging === ‘function‘ ){        choir.push( animal );        console.log( ‘恭喜加入合唱团‘ );        console.log( ‘合唱团已有成员数量:‘ + choir.length );    }};joinChoir( duck ); // 恭喜加入合唱团joinChoir( chicken ); // 恭喜加入合唱团

We see that for the choir-joined animals, there is no need to check their type, but to ensure that they have the ducksinging method. If the next time you want to join the choir is a puppy, and the puppy will be a duck bark, the puppy can join the smooth.

In the object-oriented design of dynamic type language, the concept of duck type is very important. Using the duck type idea, we can easily implement a principle in a dynamic type language without the help of a super type: "interface-oriented programming, not implementation-oriented programming." For example, if an object has a push and pop method, and these methods provide the correct implementation, it can be used as a stack. An object can be used as an array if it has the length property, or if the attribute is accessed according to the subscript (preferably with slice and splice, etc.).

In statically typed languages, it is not an easy task to implement "interface-oriented programming", which tends to transform objects upward through abstract classes or interfaces. When the object's true type is hidden behind its superclass, these objects can be replaced with each other under "monitoring" of the type-checking system. The value of object polymorphism can be manifested only when the object can be substituted with each other.

"Interface-oriented programming" is the most important idea in design patterns, but in the JavaScript language, "interface-oriented programming" is not the same as the mainstream static type language, so the process of implementing design patterns in JavaScript is quite different from those implemented in some of the languages we are familiar with.

Object-oriented javascript---dynamic type language

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.