20 ways to summarize JavaScript creation functions

Source: Internet
Author: User
Tags object object

Http://www.jb51.net/article/68285.htm

Work often creates a function to solve some requirements problem, the following is the individual in the work summed up the creation function 20 ways, you know how much?

function SayHello () {    console.log (' Hello ');} function Leave () {    console.log (' Goodbye ');} Testsayhello ();

To complete the requirement, quickly declare a function.

var SayHello = function () {    console.log (' Hello ');} var leave = function () {    console.log (' Goodbye ');} Testleave ();

Responsive, function expression number to solve

var Action = {    sayhello:function () {        console.log (' hello ');    },    leave:function () {        Console.log (' Goodbye ');}    } Testaction.sayhello ();

Creating a Method object class looks neater

var Action = function () {}; Action.sayhello = function () {    console.log (' Hello ');} Action.leave = function () {    console.log (' Goodbye ');} Testaction.sayhello ();

To add an attribute method to a monomer, purify the namespace

var Action = function () {    return {        sayhello:function () {            console.log (' hello ');        },        leave:function () {            console.log (' Goodbye ');}}}    TestVar a = Action (); A.leave ();

Return new object We have more things to do.

var Action = function () {}; Action.prototype.sayHello = function () {    console.log (' Hello ');} Action.prototype.leave = function () {    console.log (' Goodbye ');} TestVar a = new Action (); A.sayhello ();

The prototype chain points to prevent creating multiple

var Action = function () {}; Action.prototype = {    sayhello:function () {        console.log (' hello ');    },    leave:function () {        Console.log (' Goodbye ');}    } TestVar a = new Action (); A.leave ();

Objects are assigned to prototypes that look neater

var Action = function () {    This.sayhello = function () {        console.log (' hello ');    }    This.leave = function () {        console.log (' Goodbye ');}    } TestVar a = new Action (); A.leave ();

Don't forget, you can also add properties inside the class

Function.prototype.sayHello = function () {    console.log (' Hello ');} Function.prototype.leave = function () {    console.log (' Leave ');} TestVar f = function () {};f.sayhello ();

Base class prototype expansion, a new piece of space

Function.prototype.addMethod = function (name, FN) {    this[name] = fn;} var methods = function () {};methods.addmethod (' SayHello ', function () {    console.log (' Hello ');}); Methods.addmethod (' Leave ', function () {    console.log (' Leave ');}); /testmethods.sayhello ();

Common definition method functions are more convenient to use

Function.prototype.addMethod = function (name, FN) {    this.prototype[name] = fn;} var Methods = function () {}; Methods.addmethod (' SayHello ', function () {    console.log (' Hello ');}); Methods.addmethod (' Leave ', function () {    console.log (' Leave ');}); /testvar a = new Methods (); A.leave ();

Prototype assignment we can also use class operations

Function.prototype.addMethod = function (name, FN) {    this[name] = fn;    return this;} var methods = function () {};methods.addmethod (' SayHello ', function () {    console.log (' Hello ');}). Addmethod (' Leave ', function () {    console.log (' Leave ');}); /testmethods.leave ();

What is the chain operation?

Function.prototype.addMethod = function (name, FN) {    this.prototype[name] = fn;    return this;} var Methods = function () {}; Methods.addmethod (' SayHello ', function () {    console.log (' Hello ');}). Addmethod (' Leave ', function () {    console.log (' Leave ');}); /testvar a = new Methods (); A.leave ();

Prototype + chain = further

Function.prototype.addMethod = function (obj) {for    (var key in obj) {        This[key] = Obj[key];}    } var methods = function () {};methods.addmethod ({    sayhello:function () {        console.log (' hello ');    },    Leave:function () {        console.log (' goodbye ');    }}); /testmethods.leave ();

Add an object to do more at once

Function.prototype.addMethod = function (obj) {for    (var key in obj) {        This.prototype[key] = Obj[key];}    } var Methods = function () {}; Methods.addmethod ({    sayhello:function () {        console.log (' hello ');    },    leave:function () {        Console.log (' Goodbye ');}    ); /testvar a = new Methods (); A.leave ();

What does the prototype have to do?

Function.prototype.addMethod = function (obj) {for    (var key in obj) {        This[key] = Obj[key];    }    return this;} var methods = function () {};methods.addmethod ({    sayhello:function () {        console.log (' hello ');    }}). Addmethod ({    leave:function () {        console.log (' goodbye ');    }}); /testmethods.leave ();

Functional add-on objects can also be chained

Function.prototype.addMethod = function (obj) {for    (var key in obj) {        This.prototype[key] = Obj[key];    }    return this;} var Methods = function () {}; Methods.addmethod ({    sayhello:function () {        console.log (' hello ');    }}). Addmethod ({    leave:function () {        console.log (' goodbye ');    }}); /testvar a = new Methods (); A.leave ();

Chained operations of the class can also be done more

Function.prototype.addMethod = function () {    if (Arguments.length < 1)        return;    var tostring = Object.prototype.toString;    if (Tostring.call (arguments[0]) = = = ' [Object Object] ') {for        (var key in arguments[0]) {            This[key] = arguments[0 ][key];        }    } else if (typeof arguments[0] = = = "string" && Tostring.call (arguments[1]) = = = ' [object Function] ') {        this[ Arguments[0]] = arguments[1];    }    return this;}

function add encapsulate

Function.prototype.addMethod = function () {    if (Arguments.length < 1)        return;    var tostring = Object.prototype.toString;    if (Tostring.call (arguments[0]) = = = ' [Object Object] ') {for        (var key in arguments[0]) {            This.prototype[key] = Arguments[0][key];        }    } else if (typeof arguments[0] = = = "string" && Tostring.call (arguments[1]) = = = ' [object Function] ') {        This.prototype[arguments[0]] = arguments[1];    }    return this;}

Class added is the pursuit of personalization

 Function.prototype.addMethod = function () {if (Arguments.length < 1) return;    var cout = 0, tostring = Object.prototype.toString, that;        if (typeof arguments[0] = = = "Boolean" && Arguments[0]) {cout++;    that = this;    }else{that = This.prototype; } if (Tostring.call (arguments[cout]) = = = ' [Object Object] ') {for (var key in Arguments[cout]) {That[key        ] = Arguments[cout][key];        }}else if (typeof arguments[cout] = = = "string" && Tostring.call (arguments[cout + 1]) = = = ' [object Function] ') {    That[arguments[cout]] = arguments[cout + 1]; } return this; Textvar Text1 = function () {}; Text1.addmethod (' SayHello ', function () {Console.log (' last Say Hello! ')}). Addmethod (' Leave ', function () {Console.log (' last goodbye! ')}); var t = new Text1 (); T.sayhello (); T.leave (); var test2 = function () {};test2.addmethod (True, ' SayHello ', function () { Console.log (' Last Say Hello! ')}). Addmethod (True, ' leave ', functiOn () {Console.log (' last goodbye! ')}); Test2.sayhello (); Test2.leave ();

The pursuit of personalization, so do not say why

20 ways to summarize JavaScript creation functions

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.