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