Javascript design pattern-expressive JavaScript (1)

Source: Internet
Author: User

 

 

 

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)

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.