SlideShare a Scribd company logo
1 of 18
Click to add Text 
Javascript 
Presented by Bunlong VAN 
http://geekhmer.github.io 
Basic
The Disclaimer
Data Types 
•Object 
•Function 
•Number 
•Strings 
•Booleans 
•Null – a values isn't anything 
•Underfined – default value for variables and parameters, value 
of missing members in the object etc.
typeof Prefix Operator 
•The typeof prefix operator returns a string identifying the type 
of value 
•Use instanceof instead 
typeof(true); // “boolean” 
[] instanceof Array; // true
Object 
•Objects can contain data and methods 
•Objects can inherit from other objects 
•An object contain an unordered collection of name/value pairs 
•Values can be any type including other objects 
•JSON
Object Literals 
•Object are wrapped in {} 
•Names can be string 
•Value can be an expression 
•; used for separation 
•, separate pairs 
var myObject = { 
name: “Javascript”, 
“data”: { foo: 10, bar: 20 } 
} 
var name = myObject.name; 
var foo = myObject.data.foo;
Array 
•Array inherits from Object (like every other object in 
JavaScript) 
•No need to provide length when creating a new one 
•Unlike object they have a length member and is always 1 larger 
than the highest subscript 
concat, join (can also be used for concatenating multiple strings), 
pop, push, slice, sort, splice
Array (Con.) 
• Use construction 
value.constructor === Array 
• Use instanceof 
value instanceof Array 
[].constructor === Array; // true 
[] instanceof Array; // true
Function 
•They are first class Objects 
•Can be passed, stored and returned just like any value 
•Inherit from Object 
•Function can appear anywhere an expression can appear
Closure (Function Con.) 
•Function can be contained inside other functions 
•Inner function has access to the variable and parameters of the 
function it is contained within 
•This is static or lexical scoping 
•The scope that inner function has access to continues even after 
the parent function has returned is called Closure 
function sayHello(name) { 
var text = 'Hello ' + name; 
var sayAlert = function() { alert(text); }; 
return sayAlert; 
} 
var say = sayHello(“Foo”); 
say();
Function (Con.) 
•Function inside an object is called a method 
•When invoked with too many arguments, the rest are 
ignored 
•When invoked with fewer arguments, the rest are set to 
undefined 
var showMe = function(foo, bar) { 
return foo + bar; 
} 
showMe(“foo”, “bar”, “foobar”); // 3rd argument will be ignored 
showMe(“foo”); // bar will be undefined
Function (Con.) 
•When a function is invoked in method format, this refers to the 
object containing it. 
var foo = { 
baz: 10, 
bar: function() { 
console.log(this.baz); 
} 
}; 
foo.bar();
Function (Con.) 
•When object is invoked in function, this refers to the global 
object. 
var globalVariable = 5; 
function test() { 
console.log(this.globalVariable); 
} 
test
Function (Con.) 
•When new function is used (implicit return is optional), then this 
returns the new object created 
var Test = function(id) { 
this.something = id; 
this.foo = function() { 
console.log(“this is a test: ” + this.something); 
} 
} 
var t = new Test(10), 
t1 = new Test(11); 
t.foo(); 
t1.foo();
Function (Con.) 
•When function is invoked, in addition to its parameters it has a 
special parameter called arguments 
•Contains all arguments from invocation 
•Arguments.length will tell you how many arguments were 
passed 
•Arguments is not of type Array even 
•Though it has length 
var foo = function() { 
var a = new Array(); 
console.log(typeof a); 
console.log(typeof arguments); 
console.log(arguments instanceof Object); 
console.log(arguments instanceof Array) 
} 
foo();
global object 
•As crockford says, the object that dare not speak of its 
name 
•It is the container for all global variables and all built in 
objects 
•On browsers window is the global object 
•Variable defined with a var makes it local to the scope
global variables are evil 
•Un-namespaced values can clash each others values 
•Use of it should be minimized or gotten rid of totally 
•Variables defined in the function / module should start with var 
otherwise they have a global scope
Basic Javascript

More Related Content

What's hot (20)

Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Angular 2 Essential Training
Angular 2 Essential Training Angular 2 Essential Training
Angular 2 Essential Training
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
An Introduction to the DOM
An Introduction to the DOMAn Introduction to the DOM
An Introduction to the DOM
 
ASP.NET Lecture 1
ASP.NET Lecture 1ASP.NET Lecture 1
ASP.NET Lecture 1
 
Object oriented programming With C#
Object oriented programming With C#Object oriented programming With C#
Object oriented programming With C#
 
ASP.NET Page Life Cycle
ASP.NET Page Life CycleASP.NET Page Life Cycle
ASP.NET Page Life Cycle
 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
 
Xml parsers
Xml parsersXml parsers
Xml parsers
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
 
php
phpphp
php
 
Xpath presentation
Xpath presentationXpath presentation
Xpath presentation
 
Retrieving data using the sql select statement
Retrieving data using the sql select statementRetrieving data using the sql select statement
Retrieving data using the sql select statement
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
 
PHP slides
PHP slidesPHP slides
PHP slides
 
PLSQL
PLSQLPLSQL
PLSQL
 
ADO.NET
ADO.NETADO.NET
ADO.NET
 
Jquery
JqueryJquery
Jquery
 

Viewers also liked

Viewers also liked (8)

Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic course
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Java script ppt
Java script pptJava script ppt
Java script ppt
 
Js ppt
Js pptJs ppt
Js ppt
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
JakartaJS - How I Learn Javascript From Basic
JakartaJS - How I Learn Javascript From BasicJakartaJS - How I Learn Javascript From Basic
JakartaJS - How I Learn Javascript From Basic
 

Similar to Basic Javascript

JavaScript: The Language
JavaScript: The LanguageJavaScript: The Language
JavaScript: The LanguageEngage Software
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almostQuinton Sheppard
 
Introduction to javascript and yoolkui
Introduction to javascript and yoolkuiIntroduction to javascript and yoolkui
Introduction to javascript and yoolkuiKhou Suylong
 
Front end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript coreFront end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript coreWeb Zhao
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3Ahmet Bulut
 
Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02plutoone TestTwo
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java scriptvivek p s
 
JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course yoavrubin
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: PrototypesVernon Kesner
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP FunctionsAhmed Swilam
 

Similar to Basic Javascript (20)

JavaScript: The Language
JavaScript: The LanguageJavaScript: The Language
JavaScript: The Language
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almost
 
Intermediate JavaScript
Intermediate JavaScriptIntermediate JavaScript
Intermediate JavaScript
 
Javascript
JavascriptJavascript
Javascript
 
Introduction to javascript and yoolkui
Introduction to javascript and yoolkuiIntroduction to javascript and yoolkui
Introduction to javascript and yoolkui
 
Front end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript coreFront end fundamentals session 1: javascript core
Front end fundamentals session 1: javascript core
 
Linq Introduction
Linq IntroductionLinq Introduction
Linq Introduction
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
 
About Python
About PythonAbout Python
About Python
 
Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02Prototype 120102020133-phpapp02
Prototype 120102020133-phpapp02
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java script
 
Ajaxworld
AjaxworldAjaxworld
Ajaxworld
 
Prototype
PrototypePrototype
Prototype
 
JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: Prototypes
 
Functions1
Functions1Functions1
Functions1
 
functionnotes.pdf
functionnotes.pdffunctionnotes.pdf
functionnotes.pdf
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
 
JavaScript Primer
JavaScript PrimerJavaScript Primer
JavaScript Primer
 

More from Bunlong Van

scrummethodology-151002092252-lva1-app6891
scrummethodology-151002092252-lva1-app6891scrummethodology-151002092252-lva1-app6891
scrummethodology-151002092252-lva1-app6891Bunlong Van
 
Scrum methodology
Scrum methodologyScrum methodology
Scrum methodologyBunlong Van
 
Javascript dom event
Javascript dom eventJavascript dom event
Javascript dom eventBunlong Van
 
Javascript Object Oriented Programming
Javascript Object Oriented ProgrammingJavascript Object Oriented Programming
Javascript Object Oriented ProgrammingBunlong Van
 
Real time web and mobile application with Erlang & Ruby programming language
Real time web and mobile application with Erlang & Ruby programming languageReal time web and mobile application with Erlang & Ruby programming language
Real time web and mobile application with Erlang & Ruby programming languageBunlong Van
 
Ruby on Rails testing with Rspec
Ruby on Rails testing with RspecRuby on Rails testing with Rspec
Ruby on Rails testing with RspecBunlong Van
 
How to develop app for facebook fan page
How to develop app for facebook fan pageHow to develop app for facebook fan page
How to develop app for facebook fan pageBunlong Van
 

More from Bunlong Van (9)

scrummethodology-151002092252-lva1-app6891
scrummethodology-151002092252-lva1-app6891scrummethodology-151002092252-lva1-app6891
scrummethodology-151002092252-lva1-app6891
 
Scrum methodology
Scrum methodologyScrum methodology
Scrum methodology
 
Javascript dom event
Javascript dom eventJavascript dom event
Javascript dom event
 
Javascript Object Oriented Programming
Javascript Object Oriented ProgrammingJavascript Object Oriented Programming
Javascript Object Oriented Programming
 
Real time web and mobile application with Erlang & Ruby programming language
Real time web and mobile application with Erlang & Ruby programming languageReal time web and mobile application with Erlang & Ruby programming language
Real time web and mobile application with Erlang & Ruby programming language
 
Why ruby?
Why ruby?Why ruby?
Why ruby?
 
Ruby on Rails testing with Rspec
Ruby on Rails testing with RspecRuby on Rails testing with Rspec
Ruby on Rails testing with Rspec
 
How to develop app for facebook fan page
How to develop app for facebook fan pageHow to develop app for facebook fan page
How to develop app for facebook fan page
 
Why less?
Why less?Why less?
Why less?
 

Recently uploaded

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 

Recently uploaded (20)

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 

Basic Javascript

  • 1. Click to add Text Javascript Presented by Bunlong VAN http://geekhmer.github.io Basic
  • 3. Data Types •Object •Function •Number •Strings •Booleans •Null – a values isn't anything •Underfined – default value for variables and parameters, value of missing members in the object etc.
  • 4. typeof Prefix Operator •The typeof prefix operator returns a string identifying the type of value •Use instanceof instead typeof(true); // “boolean” [] instanceof Array; // true
  • 5. Object •Objects can contain data and methods •Objects can inherit from other objects •An object contain an unordered collection of name/value pairs •Values can be any type including other objects •JSON
  • 6. Object Literals •Object are wrapped in {} •Names can be string •Value can be an expression •; used for separation •, separate pairs var myObject = { name: “Javascript”, “data”: { foo: 10, bar: 20 } } var name = myObject.name; var foo = myObject.data.foo;
  • 7. Array •Array inherits from Object (like every other object in JavaScript) •No need to provide length when creating a new one •Unlike object they have a length member and is always 1 larger than the highest subscript concat, join (can also be used for concatenating multiple strings), pop, push, slice, sort, splice
  • 8. Array (Con.) • Use construction value.constructor === Array • Use instanceof value instanceof Array [].constructor === Array; // true [] instanceof Array; // true
  • 9. Function •They are first class Objects •Can be passed, stored and returned just like any value •Inherit from Object •Function can appear anywhere an expression can appear
  • 10. Closure (Function Con.) •Function can be contained inside other functions •Inner function has access to the variable and parameters of the function it is contained within •This is static or lexical scoping •The scope that inner function has access to continues even after the parent function has returned is called Closure function sayHello(name) { var text = 'Hello ' + name; var sayAlert = function() { alert(text); }; return sayAlert; } var say = sayHello(“Foo”); say();
  • 11. Function (Con.) •Function inside an object is called a method •When invoked with too many arguments, the rest are ignored •When invoked with fewer arguments, the rest are set to undefined var showMe = function(foo, bar) { return foo + bar; } showMe(“foo”, “bar”, “foobar”); // 3rd argument will be ignored showMe(“foo”); // bar will be undefined
  • 12. Function (Con.) •When a function is invoked in method format, this refers to the object containing it. var foo = { baz: 10, bar: function() { console.log(this.baz); } }; foo.bar();
  • 13. Function (Con.) •When object is invoked in function, this refers to the global object. var globalVariable = 5; function test() { console.log(this.globalVariable); } test
  • 14. Function (Con.) •When new function is used (implicit return is optional), then this returns the new object created var Test = function(id) { this.something = id; this.foo = function() { console.log(“this is a test: ” + this.something); } } var t = new Test(10), t1 = new Test(11); t.foo(); t1.foo();
  • 15. Function (Con.) •When function is invoked, in addition to its parameters it has a special parameter called arguments •Contains all arguments from invocation •Arguments.length will tell you how many arguments were passed •Arguments is not of type Array even •Though it has length var foo = function() { var a = new Array(); console.log(typeof a); console.log(typeof arguments); console.log(arguments instanceof Object); console.log(arguments instanceof Array) } foo();
  • 16. global object •As crockford says, the object that dare not speak of its name •It is the container for all global variables and all built in objects •On browsers window is the global object •Variable defined with a var makes it local to the scope
  • 17. global variables are evil •Un-namespaced values can clash each others values •Use of it should be minimized or gotten rid of totally •Variables defined in the function / module should start with var otherwise they have a global scope