JavaScript function
Define a functionfunction f(msg) {alert(msg);}// overridefunction f(msg1, msg2) {alert(msg1 + ' ' + msg2);}// arguments keywordfunction f() {    for (var i = 0; i < arguments.length; i ++ ) {        alert(arguments[i]);}}// callf();
Define a classvarMyAlert = function (msg) {   this.msg = msg;// define a methodthis.f = function () {        alert('Hello ' + this.msg);   };};varmyalert = new MyAlert('Mary');// call f methodmyalert.f();
Define a class by prototypevarMyAlert = function (msg) {   this.msg = msg;};// use prototype to define a methodMyAlert.prototype.f = function () {   alert(this.msg);};prototype definition uses less memory
Built-in typesNumbervarnum = new Number(2); or  varnum= 2; Objectvarobj = new Object(); or  varobj = {}; Arrayvararr = new Array(1, 2, 3); or vararr = [1, 2, 3];Stringvarstr = new String('Hello'); or varstr = 'Hello';Regular expressionvar regex = new RegExp('\s*'); or varregex = /\s*/;etc... - w3schools.com/js
String prototypeString.prototype.f = function () {   alert(this);};varstr = 'Hello Mary';str.f();
Define a function (more)// goes to top of current scopefunction f(msg) {     alert(msg);}// assignmentvarf = function (msg) {    alert(msg);};// use new keywordvar f = new Function('msg', 'alert(msg);');
Function prototypeFunction.prototype.f= function () {   alert(this.toString());};var f = function () {   alert('This is a function.');};// does NOT alert "This is a function." f.f();
Anonymous function(function (msg) {    alert('This is ' + msg);})('anonymous');(function (msg) {// this line does NOT get executed    alert('This is ' + msg);}).f();
Callbackfunction f(callback) {   callback();}function dosomething() {    alert('This is a callback function.');}f(dosomething);
Return a functionfunction f() {varnum= 1;// num can be accessed in inner functionvarinnerf = function () {alert( 'inner function called, num: ' + num);    };   return innerf;}// f2 now is a functionvarf2 = f();f2();
Closurefunction f() {varnum= 1;// num can be accessed in inner functionvarinnerf = function () {num ++;        alert( 'inner function called, num: ' + num );    };    return innerf;}// f2 now is a functionvar f2 = f();f2();
SummaryFuntionis a objectused to create classcan be passed as parametercan be returnedcreates a closure when defined in another function

Javascript Function

  • 1.
  • 2.
    Define a functionfunctionf(msg) {alert(msg);}// overridefunction f(msg1, msg2) {alert(msg1 + ' ' + msg2);}// arguments keywordfunction f() { for (var i = 0; i < arguments.length; i ++ ) { alert(arguments[i]);}}// callf();
  • 3.
    Define a classvarMyAlert= function (msg) { this.msg = msg;// define a methodthis.f = function () { alert('Hello ' + this.msg); };};varmyalert = new MyAlert('Mary');// call f methodmyalert.f();
  • 4.
    Define a classby prototypevarMyAlert = function (msg) { this.msg = msg;};// use prototype to define a methodMyAlert.prototype.f = function () { alert(this.msg);};prototype definition uses less memory
  • 5.
    Built-in typesNumbervarnum =new Number(2); or varnum= 2; Objectvarobj = new Object(); or varobj = {}; Arrayvararr = new Array(1, 2, 3); or vararr = [1, 2, 3];Stringvarstr = new String('Hello'); or varstr = 'Hello';Regular expressionvar regex = new RegExp('\s*'); or varregex = /\s*/;etc... - w3schools.com/js
  • 6.
    String prototypeString.prototype.f =function () { alert(this);};varstr = 'Hello Mary';str.f();
  • 7.
    Define a function(more)// goes to top of current scopefunction f(msg) { alert(msg);}// assignmentvarf = function (msg) { alert(msg);};// use new keywordvar f = new Function('msg', 'alert(msg);');
  • 8.
    Function prototypeFunction.prototype.f= function() { alert(this.toString());};var f = function () { alert('This is a function.');};// does NOT alert "This is a function." f.f();
  • 9.
    Anonymous function(function (msg){ alert('This is ' + msg);})('anonymous');(function (msg) {// this line does NOT get executed alert('This is ' + msg);}).f();
  • 10.
    Callbackfunction f(callback) { callback();}function dosomething() { alert('This is a callback function.');}f(dosomething);
  • 11.
    Return a functionfunctionf() {varnum= 1;// num can be accessed in inner functionvarinnerf = function () {alert( 'inner function called, num: ' + num); }; return innerf;}// f2 now is a functionvarf2 = f();f2();
  • 12.
    Closurefunction f() {varnum=1;// num can be accessed in inner functionvarinnerf = function () {num ++; alert( 'inner function called, num: ' + num ); }; return innerf;}// f2 now is a functionvar f2 = f();f2();
  • 13.
    SummaryFuntionis a objectusedto create classcan be passed as parametercan be returnedcreates a closure when defined in another function