WebFund 2013F Lecture 4: Difference between revisions
No edit summary |
|||
Line 21: | Line 21: | ||
* functions as objects | * functions as objects | ||
* function literals | * function literals | ||
==notes== | |||
September 18 | |||
* empty object | |||
** foo={ }; | |||
* delegation : waiting for another object to do your work | |||
** similar to inheritance in java | |||
* delegation of objects happens in the prototype property | |||
* every object has a default prototype | |||
* everything delegates to object | |||
** similar to inherits but there are no classes and no hierarchy | |||
* object reflection | |||
** look inside an object and see what's there | |||
* for loops in javascript | |||
for(p in foo){ | |||
} | |||
** similar to a for each loop in java | |||
** will iterate through all the properties in foo, no fixed order | |||
** [[DO NOT DO THIS]] | |||
** follows prototype and looks at the properties in the entire prototype chain | |||
* typeof(foo.x) can tell you if x is | |||
** number | |||
** string | |||
** object | |||
* foo.hasOwnProperty('x') returns true or false | |||
* for loops that don't care about the prototype chain | |||
** create an array that has the properties you want and iterate through them | |||
* vector array uses [] instead of {} | |||
** ie. [5, hello, etc]; | |||
** use an array because they're ordered! | |||
* foo.x is equivalent to foo["x"] | |||
** objects are like associative arrays where you index by letters/strings | |||
==FUNCTIONS== | |||
* functions are an object | |||
* scope! | |||
** scoping rules : visibility of identifiers | |||
*** variables | |||
*** constants | |||
*** functions | |||
*** objects etc | |||
===2 ways for scoping=== | |||
* dynamic | |||
** symbols you can access and the meaning of them depends on what code has been run before hand | |||
** x=7; | |||
** x=3; | |||
** x is what part came first | |||
** global variables are inheritently global scoped | |||
* lexical/textual | |||
*** variables it can see depends on what is around it | |||
*** idea of local variables | |||
* javascript has a very important global score | |||
* create a namespace to keep global script from getting too big | |||
** but doesn't exist in a standardized form | |||
* global abatement - minimize what you write in the global scope | |||
* local variable, say var in front of it | |||
* no block scope in javascript, function scope is not block scope | |||
f = function(a){ | |||
return function(b)[ | |||
return a+b; | |||
} | |||
}; // data encapsulation | |||
g = t(5); g(7); returns 12 |
Latest revision as of 21:33, 8 November 2013
Audio from the lecture given on September 18, 2013 is available here
Readings
- Chapters 3 (Objects) and 4 (Functions), JavaScript: The Good Parts
Topics
- Array versus dot notation for objects
- scoping
- var declarations
- prototype and delegation
- empty objects
- object literals
- reflection
- typeof
- hasOwnProperty
- for and object properties
- delete
- global abatement
- functions as objects
- function literals
notes
September 18
- empty object
- foo={ };
- delegation : waiting for another object to do your work
- similar to inheritance in java
- delegation of objects happens in the prototype property
- every object has a default prototype
- everything delegates to object
- similar to inherits but there are no classes and no hierarchy
- object reflection
- look inside an object and see what's there
- for loops in javascript
for(p in foo){ }
- similar to a for each loop in java
- will iterate through all the properties in foo, no fixed order
- DO NOT DO THIS
- follows prototype and looks at the properties in the entire prototype chain
- typeof(foo.x) can tell you if x is
- number
- string
- object
- foo.hasOwnProperty('x') returns true or false
- for loops that don't care about the prototype chain
- create an array that has the properties you want and iterate through them
- vector array uses [] instead of {}
- ie. [5, hello, etc];
- use an array because they're ordered!
- foo.x is equivalent to foo["x"]
- objects are like associative arrays where you index by letters/strings
FUNCTIONS
- functions are an object
- scope!
- scoping rules : visibility of identifiers
- variables
- constants
- functions
- objects etc
- scoping rules : visibility of identifiers
2 ways for scoping
- dynamic
- symbols you can access and the meaning of them depends on what code has been run before hand
- x=7;
- x=3;
- x is what part came first
- global variables are inheritently global scoped
- lexical/textual
- variables it can see depends on what is around it
- idea of local variables
- javascript has a very important global score
- create a namespace to keep global script from getting too big
- but doesn't exist in a standardized form
- global abatement - minimize what you write in the global scope
- local variable, say var in front of it
- no block scope in javascript, function scope is not block scope
f = function(a){ return function(b)[ return a+b; } }; // data encapsulation g = t(5); g(7); returns 12