Functions Julian M Bucknall CTO Functions are objects Is an encapsulation of some code Can have properties and methods of its own Can appear as a parameter can be returned from another function ID: 565356
Download Presentation The PPT/PDF document "Julian on JavaScript:" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Julian on JavaScript: Functions
Julian M Bucknall, CTOSlide2
Functions are objectsIs an encapsulation of some codeCan have properties and methods of it’s own
Can appear as a parameter, can be returned from another function
Like all objects,
functions are passed by reference not valueSlide3
Defining a functionTwo main ways:Function literal
var
f = function(arguments) { … };
var
f = function
optName
(arguments) { … };
Function statement
function f(arguments) { … }Slide4
Return value?All functions return somethingThere is no void type
“return;” will return undefined
If no return statement
Function returns undefined
Unless it’s a constructor, called via new Slide5
InvocationWhen invoked, all functions get two extra variables
this
<evil laugh>
arguments
The parameter values of the call as an array-like objectSlide6
Invocation patternsMethodFunction
Constructor
“apply”Slide7
Method invocationDefined as a method on an objectCalled via that object
this
points to the object containing the method
Like C#, reallySlide8
Function invocationDefined as a global function
Or, defined as an internal function
Called without reference to an object
this points to the Global Object
Catches everyone outSlide9
The Global ObjectHas no nameIs where all
unowned
variables (primitives, objects, functions) end up as public properties
are visible to all code!
Browser sets up a special property called window to point to the Global Object
(window is a property of the Global Object)Slide10
Constructor invocationDefined as normal functionConvention: Name has initial uppercase letter
Called with new keyword
this points to object being constructed
c
onstructor property already set
Object constructed is returned by default
No need for return statementSlide11
“apply” invocationDefined however you wantCalled via the function’s apply method
Alternately: use the call method
You get to define the this objectSlide12
ScopeScope in JavaScript is by functionNOT braces as in C#
No block scope here
The this object stays with the outer function, inner functions get their own this (usually the Global Object)
Watch out for callbacksSlide13
Scope 2A variable declared in a function is visible throughout the function
Even before it’s lexically definedSlide14
ClosuresThe “yay!” to scope’s “ow
!”Slide15
Julian M Bucknall ∙ CTO ∙ DevExpress
@
jmbucknall
julianb@devexpress.com
http
://
devexpress.com
/
julian