5 Styles of Javascript design

Source: Internet
Author: User

1. Process-oriented programming

<script>/*Start and Stop animations using functions. */ function startanimation () {//... . }function  stopanimation () {//.... }</script>

2. Creating Class Objects

<script>/*Anim class.*/varAnim =function () {//...}; Anim.prototype.start=function () {//...}; Anim.prototype.stop=function () {//...};/*Usage.*/varMyanim =NewAnim (); Myanim.start ();//...myanim.stop ();</script>

3. Encapsulating a class in a declaration

<script>/**/varfunction  () {//...  =function  () {//...  function  () {//... }}; </script>

4. Add a method

<script>/*Add a method to the Fuction object which can be used to declare methods.*//*Function.prototype.method is used to add a new method to a class, two parameters. First argument: A string that represents the name of the new method; the second argument: a function to use as a new method*/Function.prototype.method=function(name, FN) { This. prototype[name] =fn;}/*Anim Class,with methods created using a convenience method.*/varAnim =function () {//...}; Anim.method (' Start ',function () {//...}); Anim.method (' Stop ',function () {//...});</script>

5. Chained calls

<script>/*This version allows the calls to be chained.*/Function.prototype.method=function(name, FN) { This. prototype[name] =fn;return  This;//returns this so that it can be called by a chain. };/*Anim Class,with methods created using a convenience method and chaining.*/varAnim =function () {//...}; Anim.method (' Start ',function () {//...}). Method (' Stop ',function () {//... });</script>

5 Styles of Javascript design

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.