Javascript is a very bad language, I may have learned fake Javascript, ha ha, we have any recommendations, add to send that what invitation code.
This article says: you can’t understand is you SB, I write the code will be awesome.
Write a rating component on a single line
“★★★★★ ★★★★★ ★★★★★ ★★★★★ ★ “. Define a variable rate with a value from 1 to 5, then execute the code above and look at the diagram
You realize the mods and stuff are too weak
2, how to use code to call others SB
(! (~ + []) + {}) [- [~ + ""] + [[]] * [~ + []] + ~ ~! + []] + + [] ({}) [[~! + []] * ~ + []]Copy the code
To learn why:One line of JavaScript code that can be loaded
3, how to use code to prove their elegant NB
This is awesome
console.log(([][[]]+[])[+!![]]+([]+{})[!+[]+!![]])Copy the code
4. Correct postures in the way JavaScript errors are handled
And take your uncle
try {
something
} catch (e) {
window.location.href =
"http://stackoverflow.com/search?q=[js]+" +
e.message;
}Copy the code
Learn JavaScript from a line of code
[].forEach.call(? ("*"),function(a){ a.style.outline="1px solid #"+(~~(Math.random()*(1<<24))).toString(16) })Copy the code
That’s how it translates into normal language
Array.prototype.forEach.call(document.querySelectorAll('*'), #${parseInt(math.random () * math.pow (2,24)).tostring (16)} ')Copy the code
Next in the browser control watch: something magic happens
For detailed analysis, please refer to this article:Learn JavaScript from a single line of code
6. How to pick random strings gracefully
Math.random().toString(16).substring(2) // 13 bits math.random ().tostring (36).substring(2) // 11 bitsCopy the code
7, (10)[“toString”]() === “10”
Please move to:Js bizarre techniques 1
8. Anonymous functions execute automatically
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() {};
var f = function() {}();
1, function() {}();
1 ^ function() {}();
1 > function() {}();
// ...Copy the code
9. 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
var 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:
var data = void 0; // undefinedCopy the code
Void is an operator in JavaScript that does not execute an incoming operation and returns undefined. Void can be followed by (), like void(0), doesn’t that look familiar? That’s right, when you block default hits with href in HTML, you like javascript:void(0).
Of course, the use of void is not recommended for practical purposes, except for coercion-related reasons, as it is intended to be compatible with the earlier ECMAScript standard without undefined. The way void 0 is written makes the code obscure.
On how to gracefully round
Var a = = 2.33 ~ 2.33 var b | 0 var c = 2.33 > > 0Copy the code
How to gracefully implement money formatting: 1234567890 –> 1,234,567,890
To achieve with regular magic:
var test1 = '1234567890' var 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
This I take, and this you are very witty
I take
While (1) {alert(' you shut me down ')}Copy the code
You’re clever. What a cover-up
<a href="javascript:alert(' cleared successfully ');" > Clear cache </a>Copy the code
13. Comma operator
var a = 0; var b = ( a++, 99 ); console.log(a); // 1 console.log(b); / / 99Copy the code
How best to exchange values between two integers
General approach:
var a=1,b=2;
a += b;
b = a - b;
a -= b;Copy the code
The disadvantage is also obvious, integer data overflow, for 32-bit characters the maximum representation number is 2147483647, if 2147483645 and 2147483646 exchange fails. Black technology methods:
a ^= b;
b ^= a;
a ^= b;Copy the code
Ha ha, do not understand the children’s shoes suggest to remedial C language bit operation, I do not go to review, before learning embedded when learning bit operation forgot
15. Deep copy of standard JSON
var a = {
a: 1,
b: { c: 1, d: 2 }
}
var b=JSON.parse(JSON.stringify(a))Copy the code
Standard JSON objects are useful without IE, but undefined and function are ignored.
16. Convert “1” strings to numbers without using Number, parseInt, and parseFloat and methods
Ha-ha, no casts allowed, so powerful implicit conversions come to mind
var a =1
+aCopy the code
17. How to write “Hello world!”
That’s a long scroll bar
([]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[!+[]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+(!![]+[])[+[]]+([][[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]])())[+[]]+([][[]]+[])[!+[]+!![]+!![]]+(![]+[])[!+[]+!![]]+(![]+[])[!+[]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+([][[]]+[])[+[]]+([][[]]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(![]+[])[!+[]+!![]+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+([]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[!+[]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+(!![]+[])[+[]]+([][[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]])())[!+[]+!![]+!![]]+([][[]]+[])[!+[]+!![]+!![]])()([][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(![]+[])[!+[]+!![]+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+([]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[!+[]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+(!![]+[])[+[]]+([][[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]])())[!+[]+!![]+!![]]+([][[]]+[])[!+[]+!![]+!![]])()(([]+{})[+[]])[+[]]+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+[]))+([]+{})[+!![]]+(!![]+[])[+!![]]+(![]+[])[!+[]+!![]]+([][[]]+[])[!+[]+!![]]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+([][[]]+[])[+[]]+([][[]]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(![]+[])[!+[]+!![]+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+([]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[!+[]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+(!![]+[])[+[]]+([][[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]])())[!+[]+!![]+!![]]+([][[]]+[])[!+[]+!![]+!![]])()([][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(![]+[])[!+[]+!![]+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+([]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[!+[]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+(!![]+[])[+[]]+([][[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]])())[!+[]+!![]+!![]]+([][[]]+[])[!+[]+!![]+!![]])()(([]+{})[+[]])[+[]]+(!+[]+!![]+[])+(+!![]+[]))Copy the code
It works. Awesome implicit conversion
18, parseInt(0.0000008) === 8
19, ++[[]][+[]]+[+[]] == 10
Powerful implicit conversion, 23333
20, 0.1 + 0.2 == 0.3
0.1 +0.2 == 0.3 is actually not true… So that’s why the smallest unit of money in database storage is cents.
In short, the binary floating-point representations of 0.1 and 0.2 are not exact, so they are not 0.3 when added together and are close to (not equal to) 0.30000000000000004.
So, when you compare numbers, there should be a tolerance value. In ES6 this permissive value is predefined: number.epsilon.
21, the shortest code to achieve array deduplication
[...new Set([1, "1", 2, 1, 1, 3])]Copy the code
Shortly before the interview Ali asked this question, ha ha, so also write down
22, Use the shortest code to implement an array of length m(6) and values n(8)
Array(6).fill(8)Copy the code
This is short enough, it seems that I saw an interview question somewhere and thought of some API of ES6
23. Short circuit expression
conditional
Var && 1 / / same as if a = b (b) (a = 1} else {} a = b var = b | | 1 / / same as if (b) a = {b} else {1} a =Copy the code
JavaScript version of maze
Out of the Maze, 2333
25. Fetch the maximum and minimum values in an array
var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
var maxInNumbers = Math.max.apply(Math, numbers);
var minInNumbers = Math.min.apply(Math, numbers);Copy the code
26. Convert argruments objects into arrays
var argArray = Array.prototype.slice.call(arguments); ES6: var argArray = array. from(arguments)Copy the code
The Function constructor for javascript’s high-powered grid
Many JavaScript tutorials tell us not to use the constructor of the built-in object directly to create base variables, such as var arr = new Array(2); Var arr = [1, 2]; Instead of writing.
However, the Function constructor (note the uppercase Function) is a bit special. The Function constructor takes the first parameter name to pass in, and the second is the code inside the Function (expressed as a string).
var f = new Function('a', 'alert(a)'); f('jawil'); // A pop-up window will display jawilCopy the code
Isn’t it a nice way to create a function based on the content of the incoming string? !
Find a number from an array, O(n) algorithm, if not found, return null.
Normal version:
function find (x, y) {
for ( let i = 0; i < x.length; i++ ) {
if ( x[i] == y ) return i;
}
return null;
}
let arr = [0,1,2,3,4,5]
console.log(find(arr, 2))
console.log(find(arr, 8))Copy the code
The result is that the function looks like this (as if the code above is visible below, but a little different, in order to get rid of the if language and make it look more like an expression, use? Sign expression) :
/ / functional version of the const find = = > f (f (f)) (f = > (next = > (x, y, I = 0) = > (I > = x.l ength)? null : ( x[i] == y ) ? i : next(x, y, i+1))((... args) => (f(f))(... Args)) let arr = [0,1,2,3,4,5] console.log(find(arr, 2)) console.log(find(arr, 8))Copy the code
How to read and write loaded functional code
Finally advise everyone 1: mo pack force, white young head, 2333… The original post was posted on my GitHub blog (github.com/jawil/blog), you can pay attention to the latest trends, we can communicate and learn together, common progress, to the identity of learners blog, record bit by bit.