JavaScript function<br />
Define a function<br />function f(msg) {<br />alert(msg);<br />}<br />// override<br />function f(msg1, msg2) {<br />alert...
Define a class<br />varMyAlert = function (msg) {<br />   this.msg = msg;<br />// define a method<br />this.f = function (...
Define a class by prototype<br />varMyAlert = function (msg) {<br />   this.msg = msg;<br />};<br />// use prototype to de...
Built-in types<br />Number<br />varnum = new Number(2); or  varnum= 2; <br />Object<br />varobj = new Object(); or  varobj...
String prototype<br />String.prototype.f = function () {<br />   alert(this);<br />};<br />varstr = 'Hello Mary';<br />str...
Define a function (more)<br />// goes to top of current scope<br />function f(msg) { <br />    alert(msg);<br />}<br />// ...
Function prototype<br />Function.prototype.f= function () {<br />   alert(this.toString());<br />};<br />var f = function ...
Anonymous function<br />(function (msg) {<br />    alert('This is ' + msg);<br />})('anonymous');<br />(function (msg) {<b...
Callback<br />function f(callback) {<br />   callback();<br />}<br />function dosomething() {<br />    alert('This is a ca...
Return a function<br />function f() {<br />varnum= 1;<br />// num can be accessed in inner function<br />varinnerf = funct...
Closure<br />function f() {<br />varnum= 1;<br />// num can be accessed in inner function<br />varinnerf = function () {<b...
Summary<br />Funtion<br />is a object<br />used to create class<br />can be passed as parameter<br />can be returned<br />...
Upcoming SlideShare
Loading in...5
×

Javascript Function

556

Published on

Introduce javascript function, prototype, closure etc.

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
556
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Javascript Function"

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

    Clipping is a handy way to collect important slides you want to go back to later.

×