Function the Ultimate - PowerPoint PPT Presentation

1 / 78
About This Presentation
Title:

Function the Ultimate

Description:

var statement. Declares and initializes variables within a function. Types are not specified. A variable declared anywhere within a function is visible everywhere ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 79
Provided by: croc152
Category:

less

Transcript and Presenter's Notes

Title: Function the Ultimate


1
Act III
Function the Ultimate
2
function expression
  • function
  • optional name
  • parameters
  • Wrapped in parens
  • Zero or more names
  • Separated by , comma
  • body
  • Wrapped in curly braces
  • Zero or more statements

3
function expression
  • Produces an instance of a function object.
  • Function objects are first class.
  • May be passed as an argument to a function
  • May be returned from a function
  • May assigned to a variable
  • May be stored in an object or array
  • Function objects inherit from Function.prototype.

4
var statement
  • Declares and initializes variables within a
    function.
  • Types are not specified.
  • A variable declared anywhere within a function is
    visible everywhere within the function.

5
var statement
  • It gets split into two parts
  • The declaration part gets hoisted to the top of
    the function, initializing with undefined.
  • The initialization part turns into an ordinary
    assignment.
  • var myVar 0
  • var myOtherVar
  • Expands into
  • var myVar undefined
  • var myOtherVar undefined
  • myVar 0

6
function statement
  • function
  • mandatory name
  • parameters
  • Wrapped in parens
  • Zero or more names
  • Separated by , comma
  • body
  • Wrapped in curly braces
  • Zero or more statements

7
function statement
  • The function statement is a short-hand for a var
    statement with a function value.
  • function foo()
  • expands to
  • var foo function foo()
  • which further expands to
  • var foo undefined
  • foo function foo()
  • The assignment of the function is also hoisted!

8
function expressionvfunction statement
  • If the first token in a statement is function,
    then it is a function statement.

9
Scope
  • Block scope v function scope

10
Scope
  • With the var statement, blocks do not have
    scope. Only functions have scope.
  • Dont do this
  • function assure_positive(matrix, n)
  • for (var i 0 i lt n i 1)
  • var row matrixi
  • for (var i 0 i lt row.length
  • i 1)
  • if (rowi lt 0)
  • throw new Error("Negative")

11
Declare all variables at the top of the
function.Declare all functions before you call
them.The language provides mechanisms that
allow you to ignore this advice, but they are
problematic.
12
Return statement
  • return expression
  • or
  • return
  • If there is no expression, then the return value
    is undefined.
  • Except for constructors, whose default return
    value is this.

13
Dont make functions in a loop.
  • It can be wasteful because a new function object
    is created on every iteration.
  • It can be confusing because the new function
    closes over the loops variables, not over their
    current values.

14
Don't create functions in a loop
  • for (var i ...) // WRONG
  • div_id divsi.id
  • divsi.onclick function ()
  • alert(div_id)
  • divs.forEach(function (div) // RIGHT
  • div.onclick function ()
  • alert(div.id)
  • )

15
Invocation
  • The () suffix operator surrounding zero or more
    comma separated arguments.
  • The arguments will be bound to parameters.

16
Invocation
  • If a function is called with too many arguments,
    the extra arguments are ignored.
  • If a function is called with too few arguments,
    the missing values will be undefined.
  • There is no implicit type checking on the
    arguments.

17
Two pseudo parameters
  • arguments
  • this
  • Neither is recommended.

18
arguments
  • When a function is invoked, in addition to its
    parameters, it also gets a special parameter
    called arguments.
  • It contains all of the arguments from the
    invocation.
  • It is an array-like object.
  • arguments.length is the number of arguments
    passed.
  • Weird interaction with parameters.

19
Example
  • function sum()
  • var i
  • var n arguments.length
  • var total 0
  • for (i 0 i lt n i 1)
  • total argumentsi
  • return total
  • var ten sum(1, 2, 3, 4)

20
this
  • The this parameter contains a reference to the
    object of invocation.
  • this allows a method to know what object it is
    concerned with.
  • this allows a single function object to service
    many objects.
  • this is key to prototypal inheritance.

21
Invocation
  • There are four ways to call a function
  • Function form
  • functionObject(arguments)
  • Method form
  • thisObject.methodName(arguments)
  • thisObject"methodName"(arguments)
  • Constructor form
  • new FunctionObject(arguments)
  • Apply form
  • functionObject.apply(thisObject,arguments)

22
Method form
  • thisObject.methodName(arguments)
  • thisObjectmethodName(arguments)
  • When a function is called in the method form,
    this is set to thisObject, the object containing
    the function.
  • This allows methods to have a reference to the
    object of interest.

23
Function form
  • functionObject(arguments)
  • When a function is called in the function form,
    this is set to the global object.
  • That is not very useful. (Fixed in ES5/Strict)
  • An inner function does not get access to the
    outer this.
  • var that this

24
Constructor form
  • new FunctionValue(arguments)
  • When a function is called with the new operator,
    a new object is created and assigned to this.
  • If there is not an explicit return value, then
    this will be returned.
  • Used in the Pseudoclassical style.

25
new prefix operator
  • Function.prototype.new function new()
  • var that Object.create(this.prototype)
  • var result this.apply(that, arguments)
  • return (
  • typeof result "object"
  • result ! null
  • )
  • ? result
  • that

26
Apply form
  • functionObject.apply(thisObject, arguments)
  • functionObject.call(thisObject, argument)
  • A functions apply or call method allows for
    calling the function, explicitly specifying
    thisObject.
  • It can also take an array of parameters or a
    sequence of parameters.
  • Function.prototype.call function (thisObject)
  • return this.apply(thisObject, Array
  • .prototype.slice.apply(arguments, 1))

27
this
  • this is an bonus parameter. Its value depends on
    the calling form.
  • this gives methods access to their objects.
  • this is bound at invocation time.

Invocation form this
function the global object undefined
method the object
constructor the new object
apply argument
28
Recursion
  • When a function calls itself.

29
The Little Lisper
  • http//javascript.crockford.com/little.html

30
The Little Schemer
  • http//javascript.crockford.com/little.html

31
Tennents Principle of Correspondence
  • function factorial(n)
  • var result 1 // result
    variable
  • while (n gt 1)
  • result n
  • n - 1
  • return result
  • function factorial(n)
  • return (function (result) // result
    parameter
  • while (n gt 1)
  • result n
  • n - 1
  • return result
  • (1))

32
Tennents Principle of Correspondence
  • expression
  • (function ()
  • return expression
  • ())

Any expression or statement can be wrapped in an
immediately invoked function without changing
meaning
Except this arguments var functionbreak
continue return
33
Closure
  • Lexical Scoping
  • Static Scoping
  • Functions can nest.
  • Functions are values.

34
Closure
  • The context of an inner function includes the
    scope of the outer function.
  • An inner function enjoys that context even after
    the parent functions have returned.
  • Function scope works like block scope.

35
Block Scope
  • let a
  • let b
  • a
  • b
  • a

36
Function Scope
  • function green()
  • let a
  • function yellow()
  • let b
  • a
  • b
  • a

37
Function Scope
  • function green()
  • let a
  • function yellow()
  • let b
  • a
  • b
  • a

a
38
Function Scope
  • function green()
  • let a
  • function yellow()
  • let b
  • a
  • b
  • a

a
b
39
  • Lisp 1958
  • dynamic scope
  • nested functions
  • function values
  • Algol 60 1960
  • lexical scope
  • nested functions
  • functions are not values
  • C 1972
  • lexical scope
  • functions cannot nest
  • functions are values

40
Inner survives the outer
  • function green()
  • let a
  • return function yellow()
  • let b
  • a
  • b
  • a

41
Global
  • var names
  • "zero", "one", "two", "three", "four",
  • "five", "six", "seven", "eight", "nine"
  • var digit_name function (n)
  • return namesn
  • alert(digit_name(3)) // "three"

42
Slow
  • var digit_name function (n)
  • var names
  • "zero", "one", "two", "three", "four",
  • "five", "six", "seven", "eight", "nine"
  • return namesn
  • alert(digit_name(3)) // "three"

43
Closure
  • var digit_name (function ()
  • var names
  • "zero", "one", "two", "three", "four",
  • "five", "six", "seven", "eight", "nine"
  • return function (n)
  • return namesn
  • ())
  • alert(digit_name(3)) // "three"

44
Start Over
  • var names
  • "zero", "one", "two", "three", "four",
  • "five", "six", "seven", "eight", "nine"
  • var digit_name function (n)
  • return namesn
  • alert(digit_name(3)) // "three"

45
Immediate function returns a function
  • var names
  • "zero", "one", "two", "three", "four",
  • "five", "six", "seven", "eight", "nine"
  • var digit_name (function ()
  • return function (n)
  • return namesn
  • ())
  • alert(digit_name(3)) // "three"

46
Closure
  • var digit_name (function ()
  • var names
  • "zero", "one", "two", "three", "four",
  • "five", "six", "seven", "eight", "nine"
  • return function (n)
  • return namesn
  • ())
  • alert(digit_name(3)) // "three"

47
  • function fade(id)
  • var dom document.getElementById(id)
  • var level 1
  • function step()
  • var h level.toString(16)
  • dom.style.backgroundColor
  • "FFFF" h h
  • if (level lt 15)
  • level 1
  • setTimeout(step, 100)
  • setTimeout(step, 100)

48
Pseudoclassical
function Gizmo(id) this.id
id Gizmo.prototype.toString function ()
return "gizmo " this.id
49
function Gizmo(id) this.id id
Gizmo
constructor


prototype

Object
constructor
toString function

prototype

50
function Gizmo(id) this.id
id Gizmo.prototype.toString function ()
return "gizmo " this.id
Gizmo
constructor
toString function

prototype

Object
constructor
toString function

prototype

51
function Gizmo(id) this.id
id Gizmo.prototype.toString function ()
return "gizmo " this.id
Gizmo
constructor
toString function

prototype

Object
constructor
toString function

prototype

52
new Gizmo(string)
function Gizmo(id) this.id
id Gizmo.prototype.toString function ()
return "gizmo " this.id
id string

Gizmo
constructor
toString function

prototype

Object
constructor
toString function

prototype

53
Pseudoclassical Inheritance
  • If we replace the original prototype object, then
    we can inherit another object's stuff.

54
function Hoozit(id) this.id
id Hoozit.prototype new Gizmo() Hoozit.proto
type.test function (id) return this.id
id
55
function Hoozit(id) this.id id
Gizmo
constructor
toString function

prototype

Hoozit
constructor

prototype

56
function Hoozit(id) this.id
id Hoozit.prototype new Gizmo() Hoozit.proto
type.test function (id) return this.id
id
Gizmo
constructor
toString function

prototype

Hoozit
constructor

prototype

test function

57
function Hoozit(id) this.id
id Hoozit.prototype new Gizmo() Hoozit.proto
type.test function (id) return this.id
id
new Hoozit(string)
id string

Gizmo
constructor
toString function

prototype

Hoozit
prototype

test function

58
Pseudoclassical Inheritance
  • function Gizmo(id)
  • this.id id
  • Gizmo.prototype.toString function ()
  • return "gizmo " this.id
  • function Hoozit(id)
  • this.id id
  • Hoozit.prototype new Gizmo()
  • Hoozit.prototype.test function (id)
  • return this.id id

59
Prototypal Inheritance
  • var gizmo new_constructor(Object, function (id)
  • this.id id
  • ,
  • toString function ()
  • return "gizmo " this.id
  • )
  • var hoozit new_constructor(gizmo, function (id)
  • this.id id
  • ,
  • test function (id)
  • return this.id id
  • )

60
  • function new_constructor(initializer, methods,
    extend)
  • var prototype Object.create(typeof extend
    "function"
  • ? extend.prototype
  • extend)
  • if (methods)
  • methods.keys().forEach(function (key)
  • prototypekey methodskey
  • )
  • function constructor()
  • var that Object.create(prototype)
  • if (typeof initializer "function")
  • initializer.apply(that, arguments)
  • return that
  • constructor.prototype prototype
  • prototype.constructor constructor
  • return constructor

61
Function as module
  • var
  • function
  • function
  • (function ()
  • var
  • function
  • function
  • ())

62
A Module Pattern
  • var singleton (function ()
  • var privateVariable
  • function privateFunction(x)
  • ...privateVariable...
  • return
  • firstMethod function (a, b)
  • ...privateVariable...
  • ,
  • secondMethod function (c)
  • ...privateFunction()...
  • ())

63
A Module Pattern
  • (function ()
  • var privateVariable
  • function privateFunction(x)
  • ...privateVariable...
  • GLOBAL.methodical
  • firstMethod function (a, b)
  • ...privateVariable...
  • ,
  • secondMethod function (c)
  • ...privateFunction()...
  • ())

64
Module pattern is easily transformed into a
powerful constructor pattern.
65
Power Constructors
  • Make an object.
  • Object literal
  • new
  • Object.create
  • call another power constructor

66
Power Constructors
  • Make an object.
  • Object literal, new, Object.create, call another
    power constructor
  • Define some variables and functions.
  • These become private members.

67
Power Constructors
  • Make an object.
  • Object literal, new, Object.create, call another
    power constructor
  • Define some variables and functions.
  • These become private members.
  • Augment the object with privileged methods.

68
Power Constructors
  • Make an object.
  • Object literal, new, Object.create, call another
    power constructor
  • Define some variables and functions.
  • These become private members.
  • Augment the object with privileged methods.
  • Return the object.

69
Step One
  • function constructor(spec)
  • var that otherMaker(spec)

70
Step Two
  • function constructor(spec)
  • var that otherMaker(spec)
  • var member

71
Step Three
  • function constructor(spec)
  • var that otherMaker(spec)
  • var member
  • var method function ()
  • // spec, member, method
  • that.method method

72
Step Four
  • function constructor(spec)
  • var that otherMaker(spec)
  • var member
  • var method function ()
  • // spec, member, method
  • that.method method
  • return that

73
Pseudoclassical Inheritance
  • function Gizmo(id)
  • this.id id
  • Gizmo.prototype.toString function ()
  • return "gizmo " this.id
  • function Hoozit(id)
  • this.id id
  • Hoozit.prototype new Gizmo()
  • Hoozit.prototype.test function (id)
  • return this.id id

74
Functional Inheritance
  • function gizmo(id)
  • return
  • id id,
  • toString function ()
  • return "gizmo " this.id
  • function hoozit(id)
  • var that gizmo(id)
  • that.test function (testid)
  • return testid this.id
  • return that

75
Privacy
  • function gizmo(id)
  • return
  • toString function ()
  • return "gizmo " id
  • function hoozit(id)
  • var that gizmo(id)
  • that.test function (testid)
  • return testid id
  • return that

76
  • function Hoozit(id)
  • this.id id
  • Hoozit.prototype new Gizmo()
  • Hoozit.prototype.test function (id)
  • return this.id id
  • function hoozit(id)
  • var that gizmo(id)
  • that.test function (testid)
  • return testid id
  • return that
  • var my_hoozit new Hoozit("success")
  • var test my_hoozit.test
  • alert(test("success")) // B O O M !

77
  • function Hoozit(id)
  • this.id id
  • Hoozit.prototype new Gizmo()
  • Hoozit.prototype.test function (id)
  • return this.id id
  • function hoozit(id)
  • var that gizmo(id)
  • that.test function (testid)
  • return testid id
  • return that
  • var my_hoozit hoozit("success")
  • var test my_hoozit.test
  • alert(test("success")) // true

78
Next
  • Episode IV
  • The Metamorphosis of Ajax
Write a Comment
User Comments (0)
About PowerShow.com