SlideShare a Scribd company logo
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

More Related Content

PPTX
Javascript function
PPT
JavaScript Functions
PPTX
LinkedIn TBC JavaScript 100: Functions
PPTX
Java script
PDF
JavaScript Functions
PDF
03 function overloading
PPT
Unit 6 pointers
Javascript function
JavaScript Functions
LinkedIn TBC JavaScript 100: Functions
Java script
JavaScript Functions
03 function overloading
Unit 6 pointers

What's hot (20)

PPT
C++ Function
PDF
Hello Swift 3/5 - Function
PPT
Function
PPT
Function overloading(C++)
PDF
Python Functions (PyAtl Beginners Night)
PDF
Extend GraphQL with directives
PDF
OOP and FP - Become a Better Programmer
PDF
Functions in python
ODP
Clojure basics
PPTX
Functions in C++
PPTX
Function overloading
PPTX
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
PDF
Laziness, trampolines, monoids and other functional amenities: this is not yo...
PDF
From object oriented to functional domain modeling
PDF
삼성 바다 앱개발 실패 노하우 2부
PPT
OO JS for AS3 Devs
PPTX
Function C++
PDF
FP in Java - Project Lambda and beyond
PDF
How to practice functional programming in react
PDF
Introduction to ad-3.4, an automatic differentiation library in Haskell
C++ Function
Hello Swift 3/5 - Function
Function
Function overloading(C++)
Python Functions (PyAtl Beginners Night)
Extend GraphQL with directives
OOP and FP - Become a Better Programmer
Functions in python
Clojure basics
Functions in C++
Function overloading
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Laziness, trampolines, monoids and other functional amenities: this is not yo...
From object oriented to functional domain modeling
삼성 바다 앱개발 실패 노하우 2부
OO JS for AS3 Devs
Function C++
FP in Java - Project Lambda and beyond
How to practice functional programming in react
Introduction to ad-3.4, an automatic differentiation library in Haskell
Ad

Viewers also liked (20)

PDF
Function work in JavaScript
PPTX
Resthub framework presentation
PPTX
JavaScript Conditional Statements
PDF
Writing MySQL User-defined Functions in JavaScript
PPT
JavaScript Control Statements I
PDF
Loops in JavaScript
PPTX
Javascript conditional statements
PPTX
Loops in java script
PPT
JavaScript Functions
PDF
Functional Javascript
PDF
直接照会により公開された芸術監督データ
PPTX
Senior Project
PPTX
Power Notes: Measurements and Dealing with Data-2011
PPT
Photo album latest slideshow1
PPTX
Ocnele mari
PDF
推推共享介绍 副本
PPTX
Watch reviews
KEY
Linkedin
PPT
Themes ways of the world
PPTX
Module 1
Function work in JavaScript
Resthub framework presentation
JavaScript Conditional Statements
Writing MySQL User-defined Functions in JavaScript
JavaScript Control Statements I
Loops in JavaScript
Javascript conditional statements
Loops in java script
JavaScript Functions
Functional Javascript
直接照会により公開された芸術監督データ
Senior Project
Power Notes: Measurements and Dealing with Data-2011
Photo album latest slideshow1
Ocnele mari
推推共享介绍 副本
Watch reviews
Linkedin
Themes ways of the world
Module 1
Ad

Similar to Javascript Function (20)

PDF
Four Ways to add Features to Ext JS
KEY
JavaScript Classes and Inheritance
PPT
25-functions.ppt
PDF
PPTX
Ian 20150116 java script oop
PPT
Php Reusing Code And Writing Functions
DOCX
Exercícios Netbeans - Vera Cymbron
PDF
JavaScript and UI Architecture Best Practices
KEY
jQuery Namespace Pattern
PDF
Say It With Javascript
ODT
Logic Equations Resolver J Script
PDF
運用Closure Compiler 打造高品質的JavaScript
PDF
Code Generation in PHP - PHPConf 2015
PPTX
C Programming Language Part 7
PPT
Ext oo
PDF
Swift 3.0 の新しい機能(のうちの9つ)
PPTX
Ecma script6
PPT
Functional Programming In Java
PDF
The mighty js_function
Four Ways to add Features to Ext JS
JavaScript Classes and Inheritance
25-functions.ppt
Ian 20150116 java script oop
Php Reusing Code And Writing Functions
Exercícios Netbeans - Vera Cymbron
JavaScript and UI Architecture Best Practices
jQuery Namespace Pattern
Say It With Javascript
Logic Equations Resolver J Script
運用Closure Compiler 打造高品質的JavaScript
Code Generation in PHP - PHPConf 2015
C Programming Language Part 7
Ext oo
Swift 3.0 の新しい機能(のうちの9つ)
Ecma script6
Functional Programming In Java
The mighty js_function

Recently uploaded (20)

PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Cloud computing and distributed systems.
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
NewMind AI Monthly Chronicles - July 2025
Dropbox Q2 2025 Financial Results & Investor Presentation
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Review of recent advances in non-invasive hemoglobin estimation
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Network Security Unit 5.pdf for BCA BBA.
Mobile App Security Testing_ A Comprehensive Guide.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Cloud computing and distributed systems.
Understanding_Digital_Forensics_Presentation.pptx
Encapsulation_ Review paper, used for researhc scholars
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
NewMind AI Weekly Chronicles - August'25 Week I
Per capita expenditure prediction using model stacking based on satellite ima...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

Javascript Function

  • 2. 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();
  • 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 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
  • 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 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();
  • 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 objectused to create classcan be passed as parametercan be returnedcreates a closure when defined in another function