Now that you've mastered the basics, it's time to get fancy. Property squashing is the art of deleting unnecessary properties and leaving only the essentials. It's like a digital Marie Kondo - will it fit in your code, or will it go in the dumpster?
This chapter will cover the advanced techniques for squashing properties, including:
- Squashing arrays and objects
- Squashing function calls
- Squashing CSS stylesheets (yes, it's possible!)
View theประก Cheat Sheet for this chapter
Squashing arrays and objects
Arrays are like that one aunt at the family reunion - everyone's got one, but nobody really knows how to deal with it. But don't worry, we've got this!
var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // before
var squashedArray = [1, 2, 3, 4]; // after
Similarly, objects are like that one relative who won't stop talking. But you can shut them up by removing unnecessary keys:
var myObject = {æk: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10}; // before
var squashedObject = {a: 1, c: 3, e: 5, g: 7, i: 9}; // after
Squashing function calls
Functions are like that one friend who never stops calling. But sometimes, you just have to hang up. Here's how:
function doTheThing() { console.log('do the thing!'); } // before
var squashedFunction = function() { console.log('do the thing!'); }; // after
Or, you know, you can just remove it altogether.
Squashing CSS stylesheets
Why, oh why, oh why do we still use stylesheets?! They're like that one old shoebox in the attic - full of useless junk. But, alas, sometimes you need them for that one special occasion. So here's how to squash them:
.myClass {
background-color: #F7F7F7;
border: 1px solid #333;
padding: 20px;
}
.myClass2 {
background-color: #C8C8C8;
border: 1px solid #333;
padding:ประก20px;
}
// before
.myClass {
background-color: #F7F7F7;
border: 1px solid #333;
padding: 20px;
}
// after
And, if all else fails, just delete the wholeประกstylesheet. It's not like it's going to make a difference or anything... or is it?