topic 1: assigning a function to a javascript object
- Javascript has some function programming features.
- Assign a function to a variable is one example.
// procedure programming
function myFunction(p1,p2){
return p1 + p2
}
var sum = myFunction(1, 2)
console.log('1 + 2 = ' + sum)
// function programming feature - assign a function to a javascript object
var myVar = function(p1, p2){ //assign
return p1 * p2
}
var product = myVar(2, 3) //execute it
console.log('2 * 3 = ' + product)
// object programming feature
// 1. javascript has new operator
// 2. It is more involved. I'll not to create one.
// 3. If you have to deal with some, find their usages and just use them.
topic 2: exports-require
// ------- example 1 -------------------
// part2.js
module.exports = function(){
console.log('hello world fromm part2.js')
}
// app2.js
require('./part2.js')() // execute the function
// node app2.js use and run the function at one name, a function name is not needed.
// ---------------- example 2 ---------------
// with function arguments and return data
// part2x.js
module.exports = function(p1){
return p1 * p1
};
// app2x.js
var square = require('./part2x.js')
var result = square(5)
console.log('result = ' + result) // 25