Home  >  Article  >  Web Front-end  >  JavaScript Design Patterns Expressive Javascript (1)_javascript skills

JavaScript Design Patterns Expressive Javascript (1)_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:26:13864browse

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)

Copy code The code is as follows:

//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
Copy the 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 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)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn