HTML and CSS Reference
In-Depth Information
return result;
7.6 Summary
In this chapter we've seen several approaches to JavaScript object creation, and
sharing of behavior between them. We started by gaining a thorough understanding
of how JavaScript properties and the prototype chain work. We then moved on
to constructors and used them in conjunction with their prototype property
to implement an emulation of classical inheritance. Pseudo-classical inheritance
can be tempting for developers unfamiliar with prototypes and JavaScript's native
inheritance mechanism, but can lead to complex solutions that are computationally
Dispensing the constructors, we moved on to prototype-based inheritance and
explored how JavaScript allows us to work solely on objects by extending ob-
jects with other objects. By implementing a simple Object.create function, we
avoided some of the confusion introduced by constructors and were able to see
clearer how the prototype chain helps us extend the behavior of our objects.
Functional inheritance showed us how closures can be used to store state and
achieve truly private members and methods.
To wrap it all up, we looked at object composition and mixins in JavaScript,
combining all of the previously discussed patterns. Mixins are a great match for
JavaScript, and often offer a great way to share behavior between objects.
Which technique to use? The answer will vary depending on whom you ask.
There is no one right answer, because it depends on your situation. As we've seen,
there are trade-offs when choosing between a pseudo-classical approach and a func-
tional approach that will be affected by whether object creation, method invocation,
memory usage, or security is the most crucial aspect of your application. Through-
out this topic we'll see how to use a few of the techniques presented in this chapter
in real life examples.
Search WWH ::

Custom Search