Your SlideShare is downloading. ×
  • Like
Javascript Function
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Javascript Function

  • 493 views
Published

Introduce javascript function, prototype, closure etc.

Introduce javascript function, prototype, closure etc.

Published in Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
493
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
8
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. JavaScript function
  • 2. 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();
  • 3. 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();
  • 4. 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
  • 5. 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
  • 6. String prototype
    String.prototype.f = function () {
    alert(this);
    };
    varstr = 'Hello Mary';
    str.f();
  • 7. 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);');
  • 8. 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();
  • 9. Anonymous function
    (function (msg) {
    alert('This is ' + msg);
    })('anonymous');
    (function (msg) {
    // this line does NOT get executed
    alert('This is ' + msg);
    }).f();
  • 10. Callback
    function f(callback) {
    callback();
    }
    function dosomething() {
    alert('This is a callback function.');
    }
    f(dosomething);
  • 11. 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();
  • 12. 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();
  • 13. 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