WebFund 2013F Lecture 6: Difference between revisions
No edit summary |
|||
Line 14: | Line 14: | ||
** apply | ** apply | ||
* scoping, environments | * scoping, environments | ||
==Notes== | |||
September 25 | |||
* functions | |||
* this | |||
** object you are currently accessing inside of an object | |||
* 4 method invocation patterns | |||
** (only difference is how this is used) | |||
** method | |||
*** obj.method(args); | |||
**** this === obj; | |||
** function | |||
*** m = obj.method; | |||
**** m(args); | |||
** constructor | |||
*** x = new m(args); | |||
**** this === { }; //empty object | |||
**** // don't say new? it becomes the global object | |||
*** 2 ways to keep yourself out of trouble using "new" | |||
**** any function / constructor that is supposed to use "new" start it with a capital letter | |||
***** don't use new at all | |||
****** start with an empty object and return it | |||
** apply | |||
x = m.apply( this value, arg array); | |||
x = f(1) //x is a reference to h because we return it | |||
//pointer into h along with the context in which it was called | |||
var f = function (a) { | |||
var g = function (b) { | |||
return b+3 | |||
} | |||
var h = function (c){ | |||
return(a+g(c); | |||
} | |||
return h; | |||
} | |||
a = private | |||
b= private, argument scoped to function | |||
c = private, argument scoped to function | |||
f = global | |||
g = private, g&h can see each other | |||
h = private, g&h can see each other | |||
x = global, reference to h but cannot access g because have not returned it's value | |||
* if you say obj.h =x will give you access to g, then f becomes a function | |||
* assignment | |||
** no constructor because no use of "new" | |||
app.configure(function(){ | |||
app.set('views', __dirname + '/views'); | |||
app.set('view engine', 'jade'); | |||
app.use(express.bodyParser()); | |||
app.use(express.methodOverride()); | |||
app.use(app.router); | |||
app.use(express.static(__dirname + '/public')); | |||
}); | |||
* using method | |||
* app is being cast as 'views'... and set | |||
* this in this case has been assigned to app | |||
* node never blocks | |||
* symbol can refer to a function |
Latest revision as of 21:45, 8 November 2013
Audio for the lecture given on September 25, 2013 is available here.
Readings
- Chapter 4, "Functions", in Learning Node
Topics
- function invocation patterns
- method
- function
- constructor
- apply
- scoping, environments
Notes
September 25
- functions
- this
- object you are currently accessing inside of an object
- 4 method invocation patterns
- (only difference is how this is used)
- method
- obj.method(args);
- this === obj;
- obj.method(args);
- function
- m = obj.method;
- m(args);
- m = obj.method;
- constructor
- x = new m(args);
- this === { }; //empty object
- // don't say new? it becomes the global object
- 2 ways to keep yourself out of trouble using "new"
- any function / constructor that is supposed to use "new" start it with a capital letter
- don't use new at all
- start with an empty object and return it
- don't use new at all
- any function / constructor that is supposed to use "new" start it with a capital letter
- x = new m(args);
- apply
x = m.apply( this value, arg array); x = f(1) //x is a reference to h because we return it //pointer into h along with the context in which it was called var f = function (a) { var g = function (b) { return b+3 } var h = function (c){ return(a+g(c); } return h; } a = private b= private, argument scoped to function c = private, argument scoped to function f = global g = private, g&h can see each other h = private, g&h can see each other x = global, reference to h but cannot access g because have not returned it's value
- if you say obj.h =x will give you access to g, then f becomes a function
- assignment
- no constructor because no use of "new"
app.configure(function(){ app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(__dirname + '/public')); });
- using method
- app is being cast as 'views'... and set
- this in this case has been assigned to app
- node never blocks
- symbol can refer to a function