day_18. JavaScript design Patterns and development practices-object-oriented JavaScript

Source: Internet
Author: User

Design mode object-oriented dynamic type language

Programming languages are broadly categorized by data type: static type language and dynamic type language .

The static type language determines the type of the variable at compile time, while the variable type of the dynamic type language will have some type when the variable is assigned to a value when the program is run.

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 the dynamic type language to the variable type gives a lot of flexibility to the actual coding, and 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 have the method.

All of this is based on the concept of duck type , that is, if it walks like a duck and is called a duck, it is a duck .

varDuck = {ducksinging: function() {printf (' Gagaga '); } };varChicken = {ducksinging: function() {printf (' Gagaga '); } };varChoir = [];varJoinchoir = function(animal) {if(Animal &&typeofanimal.ducksinging = = ="function") {Choir.push (animal); printf"Congratulations on joining the choir."); printf"The choir already has a number"+ choir.length); }} joinchoir (duck); Joinchoir (chicken);

Thus, an important principle in dynamic type language is realized: interface-oriented programming rather than implementation-oriented programming .

Polymorphic

The actual meaning of polymorphism is that it can produce different interpretations and different execution results on objects with different scopes of operation.

 varMakeSound = function(animal) {if(AnimalinstanceofDuck) {printf (' Ggaga '); }Else if(AnimalinstanceofChicken) {printf (' GeGeGe '); } }varDuck = function() {};varChicken = function() {}; MakeSound (NewDuck ()); MakeSound (NewChicken ());

This code does achieve polymorphism, but how to add an animal, then the MakeSound function needs to be changed, which is obviously not satisfactory.

The idea behind polymorphism is to separate "What to do" from "who to do and how", that is, to separate "unchanging things" from "things that may change ."

Some rules of prototype programming paradigm
    • If the object cannot respond to a request, it will delegate the request to its own prototype

object.prototype , which is an empty object, every object we encounter in JavaScript is actually cloned from it. object.prototype is their prototype.

Although we often use new to instantiate a function, in JavaScript, when you use the new operator to invoke a function, you actually clone the object.prototype object First, and then perform some additional procedures.

We have been talking about the prototype of the object, and in terms of the real implementation of JavaScript, we cannot say that the object has a prototype, but that the constructor of the object has a prototype.

In fact, although JavaScript objects are initially Object.prototype object clones, the prototype of the object constructor is not limited to Object.prototype , but can be dynamically directed to other objects. In this way, when object a needs to borrow the ability of object B, it can selectively point the prototype of object A's constructor to object B, thus achieving the effect of inheritance.

varA= function() {}; A.prototype={name:' Sven '};varb= function() {}; B.prototype.new A ();varb=NewB ();Console. log (B.name);//output: Sven

day_18. JavaScript design Patterns and development practices-object-oriented 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.