Thursday, January 15, 2015

Javascript lessons I've learned


function IWillCallYou(){
alert(arguments[0]);
alert("there are : "+arguments.length+" arguments passed");
}

function IWillCallYou(){
alert("Who dares to call "+arguments.callee);
}



the use of self invoking function  ,
to impose variable issolation ,
awesome!

var dt = (function(){
var myclass = {
someMethod:function(){

}
}
return myclass;
})();
console.log(dt);





javascript array methods
 every = if one is false , all is gone to oblivion , it is false , everything should be true to return true
 some = its okay to make mistakes , even if one is false , besides we only need one statemetn to be true
 filter = define a method that will run and to check which one I should return , using your defintion of how I filter the data . I will run in every data inside these damn array and if I return true then the current value passed , if I dont return true then the current data will be disregarded . now the filtering is done . I will now collect all data passed using your defined way of filter and return it . the unfortunate data (those who failed to pass) , well they dont make it.
 forEach = hey just do your thing in every data inside the  array




 creating custom objects

 when creating class in javascript
 create use prototype
 to define a method

 Person.prototype.isItEfficient(){
  return true;
 }



Method leasing  , yes you heard me method leasing ..
can I use your method for a while .
besides we have the same properties that that method use

for example

var shitMan = {
hasAButtHole:true,
takeADump:function(){
if (this.hasAButtHole) {
console.log(arguments)
console.log("dumping huge shit!");
}
}
}
var cantShitButHasButtHole = {
hasAButtHole:true,
}

the magic code

shitMan.takeADump.apply(cantShitButHasButtHole);
and they live happily ever after ..


side question , what if
I want to pass argument to that freakin method ?
well you can use
arguments
arguments[0] = first argument passed


If you are a hipster and dont want to use that apply
you can use call method
shitMan.takeADump.call(cantShitButHasButtHole,["sexy arguments"]); // yup , it is almost the same to apply




Dont get fooled
arguments is not array , its an object trust me .
heres the magic code to test it

function avadaKadabra(){
console.log(  {}.toString.apply(arguments)  )

}
avadaKadabra()


so how do I treat it like its an array .
well glad you ask . you can do it like these





//TypeError: Function.prototype.apply: Arguments list has wrong type
function avadaKadabra(){
console.log(  [].join.apply(arguments,"|")  )
}
avadaKadabra("one","two")


// no error at all .
function avadaKadabra(){
console.log(  [].join.call(arguments,"|")  )
}
avadaKadabra("one","two")



want to know the difference ,
the answer is the second parameter type .
using apply , the second parameter should be an array


while using call ,
you can pass infinite arguments

to fix the error



//TypeError: Function.prototype.apply: Arguments list has wrong type
function avadaKadabra(){
console.log(  [].join.apply(arguments,["|"])  )
}
avadaKadabra("one","two")

now , whats the difference between apply and call





function something(){
if (arguments instanceOf Object) {};

popedData = Array.prototype.pop.apply(arguments);
console.log("THe poped data is : "+popedData);
console.log(arguments);
}

something(1,2,3,4,5)

No comments:

Post a Comment