Introduction:
The javascript design pattern is turing Publishing. in learning, I want to focus on every chapter.
Question:
1.1Javascript flexibility
Object-oriented JavaScript programming mode: 1. Saving state 2. methods that can be called within an object3. WriteProgramBetter understanding of its structure and able to withstand iterative development (I personally think)
// Constructor, also known as a class VaR anim = Function (){
...
} // Method in the class, prototype in Javascript
Anim. Prototype. Start = Function (){
...
}
Note: Generally, the method is put into the prototype, because the prototype generally stores things that remain unchanged and common.
Code
Function. Prototype. Method = Function (name, FN ){
This . Prototype [name] = FN;
}
VaR anim = Function (){
}
Anim. Method ( " Start " , Function () {alert ( " Started " )})
Anim. Method ( " Stop " , Function () {alert ( " Stopped " )})
VaR anim = New Anim ();
Anim. Start ();
Anim. Stop ();
The aboveCodeHighlights the following points:
1. All functions are function objects of this class, such as var f = new function ("alert ();")
2. We can also add methods to the system default class prototype, but this is not advocate and is easy to confuse.
Functions are first-class objects:
An anonymous function can create a closure (the closure will be further learned)