Why do we use Patterns? Here are some advantages:
Suppose we need to get the count of all the elements in the DOM with class 'foo', we can use a number of methods:
- Patterns help in identifying minor issues that can cause serious issues during the software development process
- Patterns provide generalized solution, not specific to one problem
- Patterns help to make the code more DRY
- Patterns add to a developers vocabulary, which makes communication faster
- Patterns that are commonly used can be improved over time, making it more efficient
Suppose we need to get the count of all the elements in the DOM with class 'foo', we can use a number of methods:
- Select all of the elements in the page and then store references to them. Next, filter this collection and use regular expressions (or another means) to only store those with the class "foo".
- Use a modern native browser feature such as querySelectorAll()
- Use native features such as getElementsByClassName()
But in jQuery, we can simply use $("selector"). This is significantly more easy to use for selecting HTML elements on a page versus having to manually handle opt for. This provides the most optimized way for accessing all the elements having a particular class or characteristics.
No comments:
Post a Comment