JavaScript Pretend Guide

JavaScript Pretend Guide

This article adheres to

If you don't understand, you are an idiot. The code I write must be awesome.

The concept of js is used to introduce some js pretending skills.

Please use the last three of the following tips with caution in team projects (mainly considering the issue of readability), otherwise your leader will punish you without any negotiation.

[[165610]]

Boolean

This technique is used a lot and is very simple.

  1. !!'foo'

By using two inversions, you can force conversion to Boolean type. This is more commonly used.

Number

This is also very simple, String converted to Number

  1. +'45'
  2. +new Date

It will be automatically converted to number type. It is commonly used.

IIFE

This is actually very practical and not a show-off. It's just that other languages ​​don't have this kind of gameplay, and it's really cool to show it to students who don't know much about JS.

  1. (function(arg) {
  2. // do something
  3. })(arg)

The practical value is that it can prevent global pollution. However, with the popularity of ES2015, there is no need to use this now. I believe that in five years, this way of writing will gradually decline.

I've been working for five years, so it's pretty good to show off in front of interns.

Closure

Closures are a very interesting part of JavaScript. The immediately executed function above is an application of closures.

If you don’t understand, go back and look up some books. There are also many discussions on Zhihu, you can go and take a look.

Closures are a sign of mastery for beginners (but they are not).

  1. var counter = function () {
  2. var count = 0  
  3. return function() {
  4. return count++
  5. }
  6. }

The closure is used above, which looks quite impressive, but it seems to have no practical value.

So what about this?

  1. var isType = function (type) {
  2. return function(obj) {
  3. return toString.call(obj) == '[Object ' + type + ']';
  4. }
  5. }

It is very easy to implement category determination through high-order functions. (Don’t forget Array.isArray() for determining Array)

Of course, it's obvious that this is just the basics and can't be more impressive. Let's look at the next section

Event

Event response frontends must be written to death, but generally speaking, how do you write a counter?

  1. var times = 0  
  2. var foo = document .querySelector('.foo')
  3. foo.addEventListener('click', function() {
  4. times++
  5. console.log(times)
  6. }, false)

It seems that there is no problem, but! Why is the variable times placed outside? It is only used once and placed outside. What if there is a naming conflict, or what if it is modified outside?

At this time, such an event monitoring code is quite powerful

  1. foo.addEventListener('click', (function() {
  2. var times = 0  
  3. return function() {
  4. times++
  5. console.log(times)
  6. }
  7. })(), false)

How about it? Doesn’t it feel different immediately? It instantly becomes more stylish!

By creating a closure, encapsulating times inside it, and then returning the function. This usage is not very common.

parseInt

High energy warning

From here on, carefully write the following code into the company code!

The parseInt function is too common, how can it be so impressive? The answer is~~

Now press F12 and copy and paste the following code in the console:

  1. ~~3.14159
  2. // = > 3
  3. ~~5.678
  4. // = > 5

This trick is very cool. The principle is that ~ is an operation called bitwise negation, which returns the inverse of the value. It is a binary operation.

The reason is that numbers in JavaScript are all of double type, and they must be converted to int when performing bitwise operations. Repeating ~ twice will return the original number.

Hex

Hexadecimal operation. It is actually a usage of Array.prototype.toString(16)

When you see this word, the first thing that comes to your mind is definitely CSS colors.

If you want to do it randomly, you can do this

  1. (~~(Math.random()*(1 < < 24 ))).toString(16)

I highly recommend reading the original link below. I learned the last three techniques there.

«

Left shift operation. This operation is very cool. Generally, those who are familiar with C will know this operation. Generally, those who have changed their careers as front-end programmers may not understand it very well (I am talking about me ☹).

This is also a binary operation. Shift the value left in binary.

Explain the operation of 1<<24 above.

In fact, it is 1 shifted left 24 bits. 00000000000000000000001 shifted left 24 bits becomes 10000000000000000000000000

Don't believe it?

Try pasting the following code in the console

  1. parseInt('1000000000000000000000000', 2) === (1 < <   twenty four )

There is actually a more understandable way to explain it.

  1. Math.pow(2,24) === (1 < <   twenty four )

Because it is a binary operation, the speed is very fast.

BTW

  1. [].forEach.call($$("*"),function(a){
  2. a.style.outline = "1px solid #" +(~~(Math.random()*(1 < < 24 ))).toString(16)
  3. })

This is how it translates into normal language

  1. Array.prototype.forEach.call(document.querySelectorAll('*'),
  2. dom = >   dom.style.outline = `1px solid #${parseInt(Math.random() * Math.pow(2,24)).toString(16)}`)

Others

I won’t introduce other things like await, Decorators, etc. which are basically understood by using TypeScript.

I wish you all more awesome play

<<:  Five effective attempts at AI technology in game development

>>:  Reading source code like this is not awesome

Recommend

An Incomplete Guide to Programmer Interviews

[[151466]] It is difficult for programmers to fin...

4 tips for landing page design!

The role of landing pages in advertising is self-...

Kaspersky Lab: IoT attacks in Q2 2018

Kaspersky Lab has released IoT attack data for th...

A set of logic that may be suitable for all new media to increase followers!

Output makes people grow, output makes people gro...

If you don't eat meat regularly, your brain will be "very damaged"

Author: Lao Ke opens his mind Audit: Superb Many ...

One case study: 5 essential tools for marketers/salespeople/operators

You have just taken over the marketing work of an...

Why has the dreaded miasma in the primeval forest disappeared now?

Your browser does not support the video tag Autho...