Week 4 Flashcards
In node.js, how is each module defined?
JS defines each module
what are local modules?
modules that are defined within your project
how do you make variables and functions that are defined in a module/file accessible to other modules?
you have to export them
what is the module.exports property used for?
to export items from the module
what is the single-responsibility principle?
“every module, class or function in a computer program should have responsibility over a single part of that program’s functionality”.
what is the most accurate phrase for defining the single responsibility principle
a function should do one thing and do it well
what does the acronym DRY stand for?
don’t repeat yourself
what does the acronym WET stand for?
write everything twice
are the module.exports property names bananable?
yes, but it makes sense to keep the property names consistent with the item names
what does node initialize the module.exports property to?
an empty object
what is the exports variable initialized to?
the module.exports property value
what are the options for exporting multiple items from a module?
- assigning an object to the module.exports property
- set properties on the module.exports object
- exports shortcut
how do you export a single item from a module?
assign module.exports to the single item
if you export just a single item from a module, can you export more items later?
no
what does it mean for a module to be dependent on another module?
the module requires something from another,
a module’s dependencies are the other modules required for it to run properly
What is the other term for a module’s dependencies?
a requirement
assign an object to the module.exports property with the properties of add and subtract
module.exports = {
add,
subtract
};
set the properties of add and subtract directly on the module.exports object
module.exports.add = add
module.exports.subtract = subtract
use the exports shortcut to add the add function as a property onto the exports object
exports.add = add
what is the format to export a single item from a module; export the multiply function
module.exports = multiply
what does the built-in require function allow us to do?
import items from a module
how does the require function work?
it takes in a relative path from the module in which require is being called (the module that you wish to export into), to the module you wish to import
when importing JS files, what are you able to omit?
the .js in the file path
how are multiple items exported from a module?
as properties of the exported object
how does the theortical process work behind exporting multiple items?
you can import the object and access the properties of the object to get the desired items
what is different about importing an item from a module that has a single export?
you can just use the require function without object destructuring; the return of the require function will be the single item that was exporter
can a folder be loaded as a module in Node.js?
yes
what must a folder loaded as a module in node.js have?
a file called index.js which will be the only file that Node.js will look for and load
what would the format be to load the index.js file in the people folder as a module be?
cost people = require(‘../people’);
is scope the same thing as contex?
no
what does scope refer to?
the visibility and avaliability of variables
what does context refer to?
the value of the this keyword when code is executed
what does the value of the this keyword depend on in method-style execution?
where and how a function is invoked
what is the format for the method-style invokation?
object.method(args)
what does using method-stlye invocation ensure?
the method will be invoked and that the this within the method will be the object that method was called on
what do functions in Node that are not defined have as their context?
the global object which is also their this
how do you indicate that you want to run Js in strict mode?
out the string “use strict” at the top of the file
what is sloppy mode?
the normal JS environment
what is the difference between JS in strict mode vs sloppy?
you cannot access the global object in strict mode, so it cannot accidentally be mutated when invoking functions in unintended contexts
in strict mode, what will functions that are not explicitly declared return?
undefined
what is the context of invoked functions that are not explicitly defined?
the global object
what is the simplest use of the bind function?
to make a function that, no matter how it is called, is called with a particular this value
what is the syntax used to bind?
let boundFunc = func.bind(context)
what does bind return and what does this mean?
an exotic function; a function with its this bound no matter where that function is invoked; this is a copy of the function that it was called on, but still a new function; bind returns the function with its context set
how many arguments can bind accept?
as many as neccessary
does the bind accept arguments that are passed in when the bound function is invoked?
yes, these areguments will be appended to the arguments passed in when bound
can you change the context of a bound function?
no