The duck choir in the JavaScript kingdom

Source: Internet
Author: User

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 advantage of a statically typed language is that the type mismatch error can be found at compile time, and the editor can help us avoid some of the errors that may occur during the program's operation. Second, if the data type is explicitly defined in the program, the compiler can also perform some optimizations on the program to improve the program execution speed.

The disadvantage of a static type language is to force programmers to write programs in accordance with strong contracts, and to prescribe data types for each variable, in the final analysis as a means to help us write high-reliability programs, rather than to write programs, after all, most people write programs to fulfill demand delivery. Second, the types of declarations add more code, and in the process of programming, these details distract the programmer's energy from thinking about the business logic.

The advantage of a dynamic type language is that 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.

The disadvantage of a dynamic type language is that it is impossible to guarantee the type of the variable, so that a type-related error can occur 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 of duck type is: "If it walks like a duck, it is a duck, then it is a duck." ”

We can learn more about duck types in 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, but on has-a, not is-a.

Below we use code to simulate this story.

varDuck = {ducksinging:function() {Console.log (' Quack '); } };varChicken = {ducksinging:function() {Console.log (' Quack '); } };varChoir = [];//Choir varJoinchoir =function(animal) {if(Animal &&typeofanimal.ducksinging = = =' function ') {Choir.push (animal); Console.log (' Congratulations to join the choir '); Console.log (' Choir already has a membership number: '+ choir.length); } }; Joinchoir (duck);//Congratulations to join the choirJoinchoir (chicken);//Congratulations to join the choir

We see that for the choir-joined animals, ministers do not have to check their type, but only to ensure that they have the ducksinging method. If the next time I want to join the choir is a puppy, and this puppy will be a duck bark, I believe this 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.


This digest is from "JavaScript design pattern and development practice"

The duck choir in the JavaScript kingdom

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.