One-line code series

Source: Internet
Author: User
One-line code series

1. One line of code visualization CSS box Layout

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

Source code explanation

First, format the Code:

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

1. Select All DOM elements on the page.
$ () Is equivalent to document. querySelectorAll (). It returns a NodeList object array, which is almost supported by modern browsers.
2. cyclically traverse DOM elements
$ ("*") 'Converts all 'dom' elements into 'nodelist' objects, but this is not a JS array, therefore, you cannot directly use '$ ("*"). the forEach () method is used for iteration, but we can use the call or apply method to use forEach.
[]. ForEach. call is equivalent to Array. prototype. forEach. call, but the former has fewer bytes.
3. Add outline to the element
The reason why we do not use border but use outline is that border affects the overall layout of the page in the CSS box model, while outline does not affect the layout of the page outside the CSS box model.
4. generate random color functions

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

Random color range:

Minimum: 000000, convert to decimal: 0
Maximum: ffffff, convert to decimal 256*256*256 = 16777216 = (1 <24)

Math. random () returns 0 ~ The floating point number of 1. Math. random () * (1 <24) returns the floating point number between () and ~~ Remove the decimal part of the floating point number and convert it to the hexadecimal color value through toString (16 ).

Additional knowledge:

'~~ 'Bit' operator, type conversion, convert to a number, equivalent to 'math. floor () ', convert a floating point number to an integer. First let's look '(! (~ + []) + {}) ''+ [] '=>' +" "'=> '0 ''~ + [] '=>'-1 ''! (~ + []) '=> 'False ''(! (~ + []) + {}) '=> "'False [object Object]"' Next, let's see '[--[~ + ""] [+ [] * [~ + [] + ~~! + [] ''[+ [] '=>' [0]'' [~ + ""] '=> '[~ 0] '=>' [-1] '[~ + ""] [+ [] '=>' [-1] [0] =>-1 ''--[~ + ""] [+ [] '=>'-2 ''[~ + [] '=>' [-1] ''-- [~ + ""] [+ [] * [~ + [] '=>'-2 * [-1] '=> '2 ''~~! + [] '=> '~~! 0' => '~~ True '=> '1 ''[--[~ + ""] [+ [] * [~ + [] + ~~! + [] '=>' [2 + 1] '=>' [3] 'and then left side '(! (~ + []) + {})[--[~ + ""] [+ [] * [~ + [] + ~~! + [] '=> "'False [object Object]" [3]' => "'s" 'then look at the right side' ({} + []) [[~! + [] * ~ + [] ''({} + []) '=>"' [Object Object] "'' [~! + [] '=> '[~! 0] '=> '[~ True] '=>' [-2] ''~ + [] '=>'-1 ''[[~! + [] * ~ + [] '=>' [-2] *-1] '=>' [2] ''({} + []) [[~! + [] * ~ + [] '=> "' [Object Object]" [2] '=> "' B "'
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.