20 ways to summarize JavaScript creation functions _javascript tips

Source: Internet
Author: User

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

function SayHello () {
    console.log (' hello ');
}
function Leave () {
    console.log (' goodbye ');
}
Test
SayHello ();

To complete the request, quickly declare a function.

 
var SayHello = function () {
    console.log (' hello ');
}
var leave = function () {
    console.log (' goodbye ');
}
Test
leave ();

The number of functions expressed to solve the requirement

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

Creating a Method object class looks neater

 
var Action = function () {};
Action.sayhello = function () {
    console.log (' hello ');
}
Action.leave = function () {
    console.log (' goodbye ');
}
Test
Action.sayhello ();

Add attribute method to monomer, purify namespace

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

Returning new objects We have more things to do.

 
var Action = function () {};
Action.prototype.sayHello = function () {
    console.log (' hello ');
}
Action.prototype.leave = function () {
    console.log (' goodbye ');
}
Test
var a = new Action ();
A.sayhello ();

Prototype chain point prevents multiple creation

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

Objects are assigned to a prototype that looks neater

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

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

 
Function.prototype.sayHello = function () {
    console.log (' hello ');
}
Function.prototype.leave = function () {
    console.log (' leave ');
}
Test
var f = function () {};
F.sayhello ();

Base class prototype expansion, a new 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 ');
});
Test
Methods.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 ');
});
Test
var a = new Methods ();
A.leave ();

We can also use class to manipulate

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 ');
});
Test
methods.leave ();

Chain Operation why not?

 
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 ');
});
Test
var 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 ');
    }
);
Test
methods.leave ();

Add objects 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 ');
    }
);
Test
var a = new Methods ();
A.leave ();

What's not to be a prototype?

 
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 ');
    }
);
Test
methods.leave ();

Function-Add objects can also be chained to an operation

 
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 ');
    }
);
Test
var a = new Methods ();
A.leave ();

The chain operation of the class can also do more

 
Function.prototype.addMethod = function () {
    if (Arguments.length < 1) return
        ;
    var tostring = Object.prototype.toString;
    if (Tostring.call (arguments[0]) = = ' [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 to add encapsulation

 
Function.prototype.addMethod = function () {
    if (Arguments.length < 1) return
        ;
    var tostring = Object.prototype.toString;
    if (Tostring.call (arguments[0]) = = ' [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;
}

The pursuit of the class-style add is 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's = this;
    }else{that = This.prototype; } if (Tostring.call (arguments[cout]) = = ' [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 to this;
//text var 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 have to say why

The above mentioned is the entire content of this article, I hope you can enjoy.

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.