JavaScript Design Pattern expressive Javascript (1) _ javascript skills

Source: Internet
Author: User
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)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.