In chapter 8, we’ll cover these topics:
Form controls
> input - fields, including text, passwords, check boxes, radio buttons, and file uploads
> select - menus for drop-down lists of options
> textarea - elements for longer text entry
> button - elements for submitting and resetting forms
Accessing form elements
Form properties and methods
Form events
Submitting a form
Retrieving and changing values from a form
Form validation
In this chapter, we’ll cover the following
An introduction to OOP -
Constructor functions
, Using classes in JavaScript
, Prototypes
, Public and private methods
, Inheritance
, Creating objects from objects
, Adding methods to built-in objects
, Mixins
, Chaining functions
, This and that
, Borrowing methods from prototypes
-- Object-oriented programming (OOP) is a way of programming that uses objects that encapsulate their
own properties and methods.
-- The main concepts of OOP are encapsulation, polymorphism and inheritance.
-- Constructor functions can be used to create instances of objects.
-- ES6 introduced class declarations that use the class keyword. These can be used in place of
constructor functions.
-- Inside a constructor function or class declaration, the keyword this refers to the object returned by
the function.
-- All instances of a class or constructor function inherit all the properties and methods of its
prototype.
-- The prototype is live, so new properties and methods can be added to existing instances.
--The prototype chain is used to find an available method. If an object lacks a method, JavaScript will
check whether its prototype
has the method. If not, it will check that function’s prototype until it finds the method or reaches the
Object constructor function.
--Private properties and methods can be created by defining variables using const and defining a
function inside a constructor function.
--These can be made public using getter and setter functions.
-- Monkey-patching is the process of adding methods to built-in objects by augmenting their prototypes.
This should be done with caution
as it can cause unexpected behavior in the way built-in objects work.
-- A mixin method can be used to add properties and methods from other objects without creating an
inheritance chain.
-- Methods can be chained together and called in sequence if they return a reference to this.
-- Polymorphism allows objects to override shared methods with a more specific implementation.
-- The value of this is not retained inside nested functions, which can cause errors. This can be worked
around by using that = this ,
using the bind(this) method and using arrow functions.
-- Methods can be borrowed from other objects.
-- Composition over inheritance is a design pattern where objects are composed from 'building-block'
objects, rather than inheriting
all their properties and methods from a parent class.
Libraries, Modular JS, MVC Frameworks, Package Managers, Optimization (via minification), Build processes using Webpack