"A string ".toLowerCase()// "a string ".toUpperCase()// "A STRING "//takes a delimiter.split("")// ['A', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', ' ']//with split ^.join()// "A string "//takes startPos endPos.substring(2,4)// "st".indexOf("str")// 2.trim()// "A string".replace("string","blah");// "A blah ".match(/a str.*/i)// "A string "//takes in a position.charAt(0)// "A".charCodeAt(0)// 65//takes in base4.toString(2)// 110
//it allows you to do many awesome things likevarQ=require("q");//chain promisespromiseOne().then(promiseTwo).then(promiseThree).then(function(){//normal anonymous function}).fail(function(error){//can handle error}).done();//Create promises from async methodsQ.ninvoke(obj,"asyncMethod",{param1:"parameter"}).then(function(resultFromAsyncMethod){//do stuff});//Use callbacks and promises togetherfunctionasync(err,val){//this is a async callbackvardeferred=Q.defer();if(err){deferred.reject(newError(err));}else{deferred.resolve(val);}returndeferred.promise;}//A value can be turned into a promiseQ({x:5}).then(function(val){//val == 5});
functionblah(){vara=1;}//is the same asvara;functionsameBlah(){a=1;}
It also hoists up function declarations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
functionouterFunc(){//How did it work?? I called innerFunc before initialization it!innerFunc();functioninnerFunc(){console.log(5);}}//is not the same asfunctionouterFunc(){x();//won't workvarx=function(){console.log(5);}x();//will work}
//instead of doing something like thisobj={...render:function(){varthat=this;//have to do this every time...this.getAsyncData(function(){that.specialFunction();that.anotherSpecialFunction();});}...};//We doobj={...render:function(){this.getAsyncData(function(){this.specialFunction();this.anotherSpecialFunction();}.bind(this));//This bind means that when getAsyncData gets called//this === obj}...};