Write a rating component on a single line
Define a variable score with a value from 1 to 5, then execute the code above and look at the graph
"★★★★★ ★★★★★ ★★★★★ ★★★★★ ★★ ".Copy the code
What plug-ins are so weak compared to this!
JavaScript errors are handled in the correct manner
try {
something
} catch (e) {
window.location.href =
"http://stackoverflow.com/search?q=[js]+" +
e.message;
}
Copy the code
Anonymous functions execute themselves
Which of these would you choose? I choose to die.
( function() {}() );
( function() {} )();
[ function() {}() ];
~ function() {}();
! function() {}();
+ function() {}();
- function() {}();
delete function() {}();
typeof function() {}();
void function() {}();
new function() {}();
new function() {};
const f = function() {}();
1, function() {}();
1 ^ function() {}();
1 > function() {}();
// ...
Copy the code
Another kind of undefined
You never need to declare a variable to be undefined because JavaScript automatically sets an unassigned variable to undefined. So if you write that in code, you’ll be despised
const data = undefined;
Copy the code
But if you are obsessive-compulsive, be sure to declare a variable that has no value for the time being and assign undefined. Here’s what you might consider:
const data = void 0; // undefined
Copy the code
On how to gracefully round
Const = const ~ ~ 2.33 b = 2.33 | 0 const c = 2.33 > > 0Copy the code
How to gracefully implement money formatting: 1234567890 –> 1,234,567,890
Implement with regulars
const test1 = '1234567890' const format = test1.replace(/\B(? =(\d{3})+(? ! \d))/g, ',') console.log(format) // 1,234,567,890Copy the code
An elegant implementation of irregularity:
function formatCash(str) { return str.split('').reverse().reduce((prev, next, index) => { return ((index % 3) ? Next: (next + ',')) + prev})} console.log(formatCash('1234567890')) // 1,234,567,890Copy the code