A summary of the key points of design pattern

Source: Internet
Author: User

The first chapter of the JavaScript that faces the object

1.1 Dynamic type language and duck type

1. Programming languages are categorized by data type: Static type language and dynamic type language.

2. Static type language: The type of the variable was determined at compile time.

Pros: (1) A type mismatch error can be found during compilation, and the editor can help us avoid some of the errors that may occur during the program's operation.

(2) If the data type is explicitly specified in the program, the compiler can also perform some optimization work on the program to improve the efficiency of execution.

Cons: (1) forcing a program ape to write a program according to a strong contract.

(2) Type declarations increase the amount of code. Programming details distract The program Ape's energy, and cannot concentrate on writing business code.

3. Dynamic type language: The data type of a variable can be determined when a variable is assigned to it when the program is run.

Pros: Write less code, be more concise, and let the program ape focus on business logic rather than grammatical details.

OK: The type of the variable cannot be guaranteed, resulting in data type-related errors that may occur during the program's run.

4. Duck type: If it walks like a duck and barks like a duck, then it is a duck.

1.2 polymorphic

1. The actual meaning of polymorphism: the same operation acts on different objects, can produce different interpretations and different execution results.

In layman's words, a method that performs the same method name on different objects can produce different execution results.

The essential thought of polymorphism: "What to Do" and "who to do and how to do" separate, that is, the "unchanging things" and "things that may change" separate.

Examples of comparisons:

No isolation, no encapsulation of code

var makesound = function (animal) {if (animal instanceof Duck) {console.log ("quack");} else if (animal instanceof Chicken) {console.log ("Cluck");}} function Duck () {};function Chicken () {};makesound (New Duck ());//Quack MakeSound (New Chicken ());//Giggle

Code that isolates unchanged, encapsulates changes

var makesound = function (animal) {animal.sound ();} function Duck () {};D Uck.prototype.sound = function () {Console.log ("quack");} function Chicken () {}; Chicken.prototype.sound = function () {Console.log ("giggle");} MakeSound (new Duck);//Quack MakeSound (new Chicken);//Giggle

Not to be continued ...

A summary of the key points of design pattern

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.