function(t,d,v,i,f){...} // function(a,b,c,d,e){...} //
in
to check for an argument. arguments.length>1||(cb=alert) // 1 in arguments||(cb=alert) //
var
var
: function(a){var b=1;...} // function(a,b){b=1;...} //
setTimeout(function(){for(var i=10;i--;)... }, a) // setTimeout(function(){for(a=10;a--;)... }, a) //
a=this.localStorage;if(a){...} // if(a=this.localStorage){...} //
var a=1,b=2,c;c=a;a=b;b=c // var a=1,b=2;a=[b,b=a][0] //
"somestring-123"
..split('-')
to get the source string.for
and while
usually take the same number of bytes, but for
gives you more control and more possibilities for assignment. while(i--){...} // for(;i--;){...} // i=10;while(i--){...} // for(i=10;i--;){...} //
for(a=[1,2,3,4,5],l=a.length,i=0;i<l;i++){b=a[i];...} // for(a=[1,2,3,4,5],i=0;b=a[i++];){...} //
for..in
with assignment to get object keys. a=[];for(b in window)a.push(window[b]) // a=[];i=0;for(a[i++]in window); //
~
c indexOf
hasAnF="This sentence has an f.".indexOf("f")>=0 // hasAnF=~"This sentence has an f.".indexOf("f") //
with(document){open();write("hello");close()} // with(document)open(),write("hello"),close() //
undefined
undefined
you can use []._
or void 0
.""._
, 1.._
and [][0]
, but they are much slower . typeof [] // typeof[] //
~~
or 0|
instead of Math.floor
rand10=Math.floor(Math.random()*10) // rand10=0|Math.random()*10 //
million=1000000 // million=1e6 //
color=0x100000 // color=1<<20 //
1/0
instead of Infinity
[Infinity,-Infinity] // [1/0,-1/0] //
a==1||console.log("not one") // ~-a&&console.log("not one") //
~
to change any value by one // i = undefined i=i||0;i++ // i=-~i //
split
method, if you use zero as a separator: 'alpha,bravo,charlie'.split(',') // 'alpha0bravo0charlie'.split(0) //
link
methodlink
method that creates an html link. html="<a href='"+url+"'>"+text+"</a>" // html=text.link(url) //
replace
and exec
methods to iterate through the rows. for(a="",i=32;i--;)a+=0 // a=Array(33).join(0) //
{n}
to shorten regular expressions. For example /\d{3}/
instead of /\d\d\d/
. And vice versa /\d\d/
instead of /\d{2}/
.eval
instead of the regular constructor: r=new RegExp("{"+p+"}","g") // r=eval("/{"+p+"}/g") //
!
with numbers to create true
and false
. [true,false] // [!0,!1] //
function(i){return function(){console.log("called "+(++i)+" times")}}(0) // (function a(){console.log("called "+(ai=-~ai)+" times")}) // 0,function a(){console.log("called "+(ai=-~ai)+" times")} //
now = +new Date() // now = +new Date //
new
keyword. r=new Regexp(".",g) // r=Regexp(".",g) // l=new Function("x","console.log(x)") // l=Function("x","console.log(x)") //
return
. return ['foo',42,'bar']; // return['foo',42,'bar']; // return {x:42,y:417}; // return{x:42,y:417}; // return .01; // return.01; //
Source: https://habr.com/ru/post/119898/
All Articles