Objects, Functions, and Classes Flashcards
two types of exports
named
default
considerations for using JS modules
the value of an imported feature is read only; it cannot be modified, but its properties can be changed (like const)
features are imported as live bindings. if the value of an imported feature changes in the source module, it is updated in the target module
when importing a feature from a module, a single dot (.) means the current directory, and two dots (..) means the parent directory
module imports are moved or hoisted to the beginning of the current scope; this means that a module can be imported anywhere in the module
decorator
wrapper function that can be used to extend the behavior of another function. it receives the original function as a parameter and returns a new function with the extended behavior.
in order to decorate a class or class method, it must be prefixed with…
@nameOfDecorator
when a class method is decorated, the decorator receives three parameters…
target, name, and descriptor
when a class is decorated, the decorator only receives…
the target parameter
property descriptor of an object’s property
can be used in a decorator to modify its attributes; for instance, the writable attribute can be set to false to make a class read only
using property descriptor
each object property has a property descriptor
method to return a property descriptor:
Object.getOwnPropertyDescriptor();
value: refers to the value of the property
writable: is true if the value of the property may be changed
configurable: true if the property descriptors of the property can be changed
enumerable: attribute is true if the property is appears during the enumeration of the object’s properties
scope
which variables can be accessed
global function block
hoisting
when a variable or function is moved to the top of their scope
asynchronous
non blocking code
some standard events
click change focus input keydown keyup load mousedown mouseup resize scroll submit
in order to use custom event…
you have to dispatch it
two ways to handle events
addEventListener() - method of the EventTarget object that allows setting up a function that is called whenever the specified event is delivered to the target
onevent handler - property of an element (for example ‘onclick’) can be assigned to a single event handler function that is called when the event occurs
considerations for using addEventListener()
it’s the more common method, lots of flexibility
target: any object that supports events, but common targets are Element, Document, and Window
parameters: the method can also accept an options object and useCapture (true or false) as parameters. the options objects represents the characteristics of the event listener
benefits: method allows adding more than a single handler for a particular event. it also allows controlling the phase (bubbling or capturing) when the listener is activated)