Javascript Notes
Javascript Notes
This defines keyword defines a variable local to the most recent scope. Unlike var, which is limited to
the scope of a function.
I.e.,
function aFunc(){
var something = 5;
if(something){
let something = 2
Console.log(something) //Would print 5. As the other something isn’t defined in this scope.
Objects
In JavaScript objects are sloppy to make. They take the form of functions, which define attributes
and some of those attributes can be assigned function pointers.
For instance
This.noise = noise;
This.size = size;
Console.log(“meow”);
};
In JavaScript some functions are immediately invoked. To do this, after defining a function put
parenthesis to pass arguments to the function, then pass the arguments and it will be called
immediately after it is defined. Useful for when certain elements on a webpage need to have the
function called immediately.
Function func(one,two){ //do something//}(varOne, varTwo); //Function called with varOne, varTwo
as arguments.
In JavaScript you can pass around function pointers, treating them like you would in C or
python.
JavaScript is a weakly typed language, so Arrays can hold any data type items.
Arrays are an object in JavaScript and have associated functions.
Closure
A closure in JavaScript is when a function is defined inside a function and that inside function
uses variables defined in the outer function. If the inside function is returned as output for
the outside function, then it will remember the variables that associated with the inside
function. I.e.,
Function func (){
Var a = 2;
Var b = 4;
Function multiply(){
Return a*b;
}
Return multiply;
}
Events
In Javascript functions can be attached to run when a certain event is thrown by the
browser. An event can be any action the browser detects. Example may be, moving the
mouse, clicking a button, moving the mouse in a certain HTML element etc. The function can
be attached several ways, one way could be “[the element].onclick = func;”.
Usually the way these should be added is with the addEventListener() function. This allows
adding of multiple functions to listen on one event, as with the prior method, only one can
listen.
The call could go something like this “[the element].addEventListener(“click”,func,false);”.
The last parameter is usually always going to be false and describes how the object will be
caught and sent to the function (if it’s required).
If func has a parameter in it with the previous call, that parameter will be the event object
generated, when the event was thrown. It will contain event specific information.
Sometimes it may be desired to pass the object that threw the event, to the function. To do
this a clever work-around is required. The function to add must be wrapped around an
anonymous function and “this” must be passed as a parameter to the function. For instance:
This makes it so that the object that threw the event will be passed. As in the context of the
anonymous function, the “this” object that is calling the function, is the object that threw
the event.
You can also pass in the event object throw too, with:
Just including e as a parameter to the anonymous function and throwing it inside the func.