12 Jan 2018
School
Department
Course
Professor

What is a callback? A name for a function that is passed to another function
-we expect it to be”called” back
Modules - JS did NOT originally have modules!
Module-node.js
const secret = “cse130 is fun!”;
exports.myVar = 42;
exports.myFunc = function(x) {
If (x === secret) {
console.log(‘yes’);
} else {
console.log(‘guess again!’);
}
//IMPLICITLY returns undefined
};
What does require() do?
Function requireMyModule() {
const exports = {};
myModule(exports); //populates this object!
return exports;
}
What are JavaScript Objects?
-maps of names or symbols (strings) to values
-can NOT be a function to a value
-object literal notation:
const obj = { x: 3, y: “w00t” }
-access:
-obj.x or obj[“x”] ← 2nd one requires lookup in a table (slowoer)
-methods are “funcion-valued properties”
-obj.f = function(y) { return this.x + y; }
What is “this”?
-the receiver
- comes from Self (Smalltalk dialect)
- thing that “receives” the message you are trying to convey
-this
points to the object which has the function as a method
const obj = {
“x-w00t”: 10, //obj.x-w00t = obj.x - w00t ← does NOT WORK!
//have to use obj[“x-w00t”]
console.log(obj.f.call({ x:45 }, 4)) //50