The javascript design pattern is turing Publishing. in learning, I want to focus on every chapter. Question:
1.1 javascript flexibility
Object-oriented Javascript programming mode: 1. Saving state 2. methods that can be called within an object 3. Better understanding of the structure of a program, able to withstand iterative development (in my opinion)
The Code is as follows:
// Create functions, also known as classes
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
The Code is as follows:
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 above code highlights 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)