Yangshun Interview Questions Flashcards
Explain Event Delegation
This is a technique used to add event listeners to a parent element instead of the descendant elements. The listener will fire whenever the event is triggered on the descendant elements due to the even bubbling up the DOM. Benefits are:
- Memory footprint goes down because only one single handler is needed on the parent element, rather than having to attach event handlers on each descendant.
- There is no need to unbind the handler from elements that are removed and to bind the event for new elements.
How does prototypal inheritance work?
All JavaScript objects have a prototype property, that is a reference to another object. When a property is accessed on an object and if the property is not found on that object, the JavaScript engine looks at the object’s prototype, and the prototype’s prototype and so on, until it finds the property defined on one of the prototypes or until it reaches the end of the prototype chain. This behavior simulates classical inheritance, but it is really more of delegation than inheritance.