Javascript Function
Upcoming SlideShare
Loading in...5
×
 

Javascript Function

on

  • 597 views

Introduce javascript function, prototype, closure etc.

Introduce javascript function, prototype, closure etc.

Statistics

Views

Total Views
597
Views on SlideShare
597
Embed Views
0

Actions

Likes
0
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Javascript Function Javascript Function Presentation Transcript

  • JavaScript function
  • Define a function
    function f(msg) {
    alert(msg);
    }
    // override
    function f(msg1, msg2) {
    alert(msg1 + ' ' + msg2);
    }
    // arguments keyword
    function f() {
    for (var i = 0; i < arguments.length; i ++ ) {
    alert(arguments[i]);
    }
    }
    // call
    f();
  • Define a class
    varMyAlert = function (msg) {
    this.msg = msg;
    // define a method
    this.f = function () {
    alert('Hello ' + this.msg);
    };
    };
    varmyalert = new MyAlert('Mary');
    // call f method
    myalert.f();
  • Define a class by prototype
    varMyAlert = function (msg) {
    this.msg = msg;
    };
    // use prototype to define a method
    MyAlert.prototype.f = function () {
    alert(this.msg);
    };
    prototype definition uses less memory
  • Built-in types
    Number
    varnum = new Number(2); or varnum= 2;
    Object
    varobj = new Object(); or varobj = {};
    Array
    vararr = new Array(1, 2, 3); or vararr = [1, 2, 3];
    String
    varstr = new String('Hello'); or varstr = 'Hello';
    Regular expression
    var regex = new RegExp('s*'); or varregex = /s*/;
    etc... - w3schools.com/js
  • String prototype
    String.prototype.f = function () {
    alert(this);
    };
    varstr = 'Hello Mary';
    str.f();
  • Define a function (more)
    // goes to top of current scope
    function f(msg) {
    alert(msg);
    }
    // assignment
    varf = function (msg) {
    alert(msg);
    };
    // use new keyword
    var f = new Function('msg', 'alert(msg);');
  • Function prototype
    Function.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();
  • Callback
    function f(callback) {
    callback();
    }
    function dosomething() {
    alert('This is a callback function.');
    }
    f(dosomething);
  • Return a function
    function f() {
    varnum= 1;
    // num can be accessed in inner function
    varinnerf = function () {
    alert( 'inner function called, num: ' + num);
    };
    return innerf;
    }
    // f2 now is a function
    varf2 = f();
    f2();
  • Closure
    function f() {
    varnum= 1;
    // num can be accessed in inner function
    varinnerf = function () {
    num ++;
    alert( 'inner function called, num: ' + num );
    };
    return innerf;
    }
    // f2 now is a function
    var f2 = f();
    f2();
  • Summary
    Funtion
    is a object
    used to create class
    can be passed as parameter
    can be returned
    creates a closure when defined in another function