These are chat archives for codefellows/sea-b15-javascript

16th
Jul 2014
Kevin Stephens
@kevinmstephens
Jul 16 2014 00:25
I did the entire thing, let me see if I have it saved on my comp still
it didn't help looking at the solution, I think all the solutions are on GitHub
that was a question...it didn't help looking at the solutions??
Kevin Stephens
@kevinmstephens
Jul 16 2014 00:30
you put all the images in app/img/phones/??
Brian Studwell
@bwstud
Jul 16 2014 02:58
cfbot image me platypus
Erik Nilsen
@enilsen16
Jul 16 2014 02:58
:worried:
Brian Studwell
@bwstud
Jul 16 2014 02:58
cfbot image me suffering
lame.
Erik Nilsen
@enilsen16
Jul 16 2014 02:59
@bwstud you see my message
Brian Studwell
@bwstud
Jul 16 2014 02:59
negative
Robert Talamantez
@Talamantez
Jul 16 2014 03:16
@kevinmstephens got it figured out- over complicating things again
Andy Ernst
@AndyCErnst
Jul 16 2014 17:03

Here's something I don't understand. Why does this

elements[i].onclick = makeHandler(i+1); 
var makeHandler = function(num) {  
              console.log("This is element #" + num);
}

behave differently than

elements[i].onclick = function(){console.log(i+1)}
This was in a code snibbit to demonstrate that you can't reference an iterator (the i) inside a for loop. The .onclick parts were inside for loops. But the part I don't get is why in the first one the function execute immediately, but in the second one it waits for the click event.
To fix this they made the function return a function
var makeHandler = function(num) {  
     return function() {
         console.log("This is element #" + num);
     };
};
Robert Talamantez
@Talamantez
Jul 16 2014 17:07
maybe you have to do a prevent default to stop the first function from running right away
Andy Ernst
@AndyCErnst
Jul 16 2014 17:07
http://jsfiddle.net/AfmA5/ if you want to mess around with it
In jQuery you would prevent default by taking in the event and calling preventDefault on it but this isn't jQuery. I don't think the event is passed anywhere and I'm not sure that's the problem
Tyler Morgan
@toastynerd
Jul 16 2014 17:13
Whenever you add () to a function that’s already declared it calls it, if it’s an anonymous function it’s a declaration and you have to add the () to call it.
you could also do something like this:
elements[i].onclick = function() { makeHandler(i+1)};
and not have the return function in there
it’s a process called currying
Andy Ernst
@AndyCErnst
Jul 16 2014 17:18
I've never really gotten my head around functions returning functions (unless they are constructors or something)
Tyler Morgan
@toastynerd
Jul 16 2014 17:19
Yeah, it’s a little weird but it lets you pass a function by reference instead of calling the function.
well, I should say it lets you passs you a function with arguments by reference
Andy Ernst
@AndyCErnst
Jul 16 2014 17:26
And Robert, I figured out how to grab the event http://jsfiddle.net/AfmA5/1/ I thought it was working differently than jQuery but you were right
Robert Talamantez
@Talamantez
Jul 16 2014 17:32
cool
Andy Ernst
@AndyCErnst
Jul 16 2014 18:13

How about this one

function addClickHandler(element) {
    element.click = function onClick(e) {
        alert("Clicked the " + element.nodeName)
    }
}

I understand how it's a circular reference with element referencing itself but I don't how how to eliminate it

Tyler Morgan
@toastynerd
Jul 16 2014 18:26
it doesn’t have to be, you can get rid of that e, you’re not using it anywhere
Anna Luisa Patiño West
@aisapatino
Jul 16 2014 19:15
@KuroFluff is the problem you're having that element is undefined or referencing the wrong thing within that function? i think you can also access the element through the jquery event object - something like e.target should work (so replace element.nodeName with e.target.nodeName)