Several methods for defining functions of JS function

Source: Internet
Author: User
Tags constructor function definition

This article mainly introduces the JS function definition functions of several methods, the need for friends can refer to the

4 ways to define functions for JS function 1. The most basic function declared as a Benbon is used.    The code is as follows: function func () {} or var func=function () {};  2. Use as a class constructor: code is as follows: function class () {} class.prototype={};    var item=new class ();    3. Use as closures: code as follows: (function () {//Standalone scope}) (); 4. Can be used as a selector: The code is as follows: Var addevent=new function () {if (!-[1,]) return function (Elem,type,func) {attachevent (Elem, ' on ' +type  , func);};  else return function (Elem,type,func) {AddEventListener (elem,type,func,false);} };//avoids repeated judgments 5. The mixed application of the above four cases: the code is as follows: Var class=new function () {var privatearg;//static private variable function privatemethod=function () {};//static Private method return function () {*/* true constructor *

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.