Main topic:
1.1 The flexibility of javascript
Object-oriented Javascript programming model: 1. Can save state 2. Has methods that can only be called inside the object 3. Can have better control over the written program Its structure can withstand iterative development (in my opinion)
//Enough to construct a function, also commonly known as a class
var Anim=function(){
...
}
//Methods in classes, prototypes in javascript
Anim .prototype.start=function(){
...
}
Note: Generally speaking, the method is put into the prototype, because the prototype generally does not store Make something universal
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 above code highlights the following points:
1 , All functions are objects of the Function class, such as var f=new Function("alert();")
2. We can also add methods to the system default class prototype, but this is not recommended, it is easy Confusion
Functions are first-class objects:
Anonymous functions can create closures (closures will be studied in another article)