function modules
  Learning Javascript foundations


            John Hunter
             Nov 2008




                   ...
Review - object module
var radio = {

 volume: 0,

 frequency: 88.0,


 changeVolume: function (direction) {

 
 if (direc...
Good OO languages should support
  encapsulation - objects should be able to
       keep some properties private.
   We ca...
function something () {

 var secretTreasure = '$$$';
}
var myTreasure = something();
console.log(myTreasure);

>>> undefin...
function functionName (arguments) {
    var localVariable;
                            scope
    return returnValue;
}

ar...
function something () {
   var secretTreasure = '$$$';
   var notSoSecretTreasure = secretTreasure;
   return notSoSecretT...
function someFunction (arguments) {
    var localVariable;
                            scope
                           cl...
function something () {
                                                secretTreasure is private

 var secretTreasure = '...
Review
object properties are public
function local variables are private
function closures provide access to local variabl...
Thanks




         10
Upcoming SlideShare
Loading in …5
×

Javascript foundations: Function modules

767
-1

Published on

This presentation forms part of a tutorial on learning Javascript foundations. It introduces the Function module - a pattern for creating objects with private members.

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

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

No notes for slide

Javascript foundations: Function modules

  1. 1. function modules Learning Javascript foundations John Hunter Nov 2008 1
  2. 2. Review - object module var radio = { volume: 0, frequency: 88.0, changeVolume: function (direction) { if (direction === 'up') this.volume += 1; else this.volume -= 1; }, changeTuner: function (direction) { if (direction === 'up') this.frequency += 4; else this.frequency -= 4; } }; 2
  3. 3. Good OO languages should support encapsulation - objects should be able to keep some properties private. We cannot create private properties in Javascript objects. But there are ways to achieve the same effect. 3
  4. 4. function something () { var secretTreasure = '$$$'; } var myTreasure = something(); console.log(myTreasure); >>> undefined how can we get the secretTreasure ? 4
  5. 5. function functionName (arguments) { var localVariable; scope return returnValue; } arguments, local variables and return values are all in the function scope 5
  6. 6. function something () { var secretTreasure = '$$$'; var notSoSecretTreasure = secretTreasure; return notSoSecretTreasure; } var myTreasure = something(); console.log(myTreasure); >>> $$$ we can get the secretTreasure ? 6
  7. 7. function someFunction (arguments) { var localVariable; scope closeure return returnValue; } returned value has access to var result = someFunction(); the function scope - even when the function has completed! 7
  8. 8. function something () { secretTreasure is private var secretTreasure = '$$$'; return { getTreasureLength: function () { returned object shares scope return secretTreasure.length; with local variables }, doubleTheTreasure: function () { secretTreasure += secretTreasure; } }; } var mine = something(); console.log(mine.getTreasureLength()); >>> 3 mine.doubleTheTreasure(); >>> 6 console.log(mine.getTreasureLength()); we can interact with private variables using public methods 8
  9. 9. Review object properties are public function local variables are private function closures provide access to local variables after the function has executed functions can provide modules with private data 9
  10. 10. Thanks 10

×