On Github vacas5 / nashjsslides
Created by Russ Anderson / @RealRealRuss
Don't linger here just get started.Advocacy Marketing platform
provide ways for fans of your brand to help spread the word about what you're doinghttps://github.com/h5bp/Front-end-Developer-Interview-Questions#js-questions
this list of interview questions compiled by the folks behind h5bp. for instance (next slide)
Ternary means it involves three things, and strings 'em all on one line so it's harder to read.
here's a question on that list
That sounds hard, and for some strange reason I'm suddenly jonesing for a Dr. Pepper.
the infamous fizz buzzI'm probably not alone
We are not paid to use every feature of the language. We are paid to write programs that work well and are free of error — Douglas Crockford well, yes. Fizzbuzz brings up a lot of emotions for developers. show quote. What's more important is what you can build, how focused you can be, and that you want to learn. But try not to get hung up on the interview part. let's just learn!
JS events fire not only on a single DOM element but on all its descendants
Good example of one that I use all the time in practice but could not have just explained from memory. show definition, reverse of this is that they bubble up. If you're clicking on an 'a' tag inside an 'li', inside a 'u l', aren't you in reality clicking on all three?Inverse of this. Also known as propogation, events on an element will "bubble up" and also fire on all parents
<form id="form"> <div class="input-group"> <label class="control-label" for="Name">Name <i class="icon-asterisk"></i></label> <input type="text" id="Name" name="Name" placeholder="John Doe, e.g." required="" pattern="[A-Za-z-0-9]+\s[A-Za-z-'0-9]+" title="firstname lastname"> </div> <div class="input-group"> <label class="control-label" for="Email">Email Address <i class="icon-asterisk"></i></label> <input type="email" id="Email" name="Email" placeholder="example@yourdomain.com" required=""> </div> <div class="input-group"> <label class="control-label" for="Tel">Phone Number</label> <input type="tel" id="Tel" name="Tel" placeholder="(555) 555-5555" pattern="([0-9]{3}) [0-9]{3}-[0-9]{4}"> </div> <div class="input-group"> <label class="control-label" for="Message">How would you like to get involved?</label> <textarea id="Message" name="Message" rows="4"></textarea> </div> <div class="input-group"> <label class="checkbox"> <input name="News" type="checkbox"> Add me to your email newsletter </label> </div> <div class="input-group"> <button type="submit" name="submit" class="btn">Submit</button> </div> </form> <script> function handleChange(event) { // track your change console.log(event.currentTarget); } var formEls = document.getElementsByTagName('input'); for (var i = 0; i < formEls.length ; i++) { formEls[i].addEventListener('change', handleChange, false); } // easier with jQuery, but... $('input').change(handleChange); </script>Here's a form with a lot of different inputs. Could try and target all the elements individually.
<script> function handleChange(event) { // track your change // this time on e.target console.log(event.target); } $('#form').change(handleChange); </script>so much easier to target the form alone
Propogation still occurs even though it's added later
<script> // I do this all the time! $('body').on('click', '#modalExit', function(e) { // exit the modal }); // even better $('#modal').on('click', '#modalExit', function(e) { }); </script>Super bonus, dynamic content. Let's say you have a super annoying modal that your client asked you to put in from jquery docs: Attaching many delegated event handlers near the top of the document tree can degrade performance No good translation, just say let's move on
function foo(){ // i pity this code }();
Immediately invoked function expression
Firefox - SyntaxError: expected expression, got ')' literally had no idea what IIFE meant; it runs itself; what is wrong with this code?function foo(){ // i am known as // a definition or statement } var foo = function() { // i am an expression // i resolve to a value, even if just "undefined" };MDN - An expression is any valid unit of code that resolves to a value. functions can either be written as simple definitions, or as expressions. difference here is that JS expects a value from the latter.
// this thing here function foo(){ }(); // is as if you function foo(){ } (); // wrote it on separate lineswriting it the first way would behave similarly to how JS would see an if statement.
(function foo(){ })();
behold the parentheses
parens here essentially turn this into an expressionControl variable scope
(function foo(){ })(); console.log(foo);Firefox - ReferenceError: foo is not defined
all variables are declared at the top of any given function scope whether you like it or not
While we're on the subject of scope. Never heard this term in my life until a few weeks ago.
1 function hoist(track) { 2 if (track === 'Down With Disease') { 3 var action = 'dance'; 4 } 5 else { 6 var action = 'skip': 7 } 8 9 return action; 10 }
line 6 error: 'action' is already defined
1 function hoist(track) { 2 var action; 3 if (track === 'Down With Disease') { 4 action = 'dance'; 5 } 6 else { 7 var action = 'skip': 8 } 9 10 return action; 11 }
var declaration "hoisted" to top
1 function hoist(track) { 2 if (track === 'Down With Disease') { 3 action = 'dance'; 4 } 5 else { 6 action = 'skip': 7 } 8 9 var action; 10 11 return action; 12 }
cue collective moan
foo = 5;Firefox - ReferenceError: foo is not defined
you forgot something
undefined is not a function. developer jokes you guys!
var foo; var bar = {}; var baz = ['jonny', 'phil', 'ed']; var qux = function() { // don't return anything }; console.log(foo, bar.name, baz[4], qux()); // all print undefined
var foo = null; console.log(foo); // prints null
Undeclared finds you. Except if you're writing in the global scope!
// global scope foo = 5; console.log(foo); // prints 5! // implied global, aka a reason people hate JS
var foo; console.log(typeof foo); // "undefined" as a string console.log(typeof bar): // undeclared, but also returns "undefined" AARGH // preferred console.log(foo === undefined); // true boolean var baz = 'undefined'; console.log(baz === undefined); // false. Hooray! maybe?typeof ignores undeclared variables
var foo = null; console.log(typeof foo); // object let that sink in for a minute // preferred console.log(foo === null); // true booleanthis is literally a bug in javascript
var foo; // undefined var bar = null; // null // compare the two console.log(foo == bar); // true booleanremember these guys?
Double equals checks for equality
Triple equals checks for equality and type
Wield accordingly.
double actually converts both to the same type before comparing. Feel free to use double equals, but when appropriate. JSHint may bark at you.
var markup = Handlebars.compile(template)(model); this.$el.html(markup); this.$el.foundation('reflow');
this would never work
best explained with this story. use foundation's form validation. have to reflow if you're adding to the dom. this would never work howevervar markup = Handlebars.compile(template)(model); this.$el.html(markup); var self = this; setTimeout(function() { self.$el.foundation('reflow'); }, 0);Defers invoking the function until the current call stack has cleared
(whatever that means)
best explained with this story. use foundation's form validation. have to reflow if you're adding to the dom. this would never work howevervar markup = Handlebars.compile(template)(model); this.$el.html(markup); var self = this; setTimeout(function() { self.$el.foundation('reflow'); }, 0);
setTimeout takes function from stack and puts in queue
setTimeout is part of the Web API, provided by your browser