Ways to define functions html
Function Declration: function functionName(param1) {} node
Anonymous Function: var functionName = function(param1) {}; express
Function Constructor: var myFunction = new Function(“a”, “b”, “return a * b”); ide
What is Javascript Event Delegate? this
To bind the event listener to the parent of an html element. The reason for doing this is to easier adding and removing event listeners. For example, li inside ul, to do event delegation. We bind the click listener to ul, and then useevent.target.nodeName to find if it’s the exact node we want to bind the event to. spa
More examples see http://davidwalsh.name/event-delegate code
Explain how this works in JavaScript htm
this point to the function itself. ip
When a function is called as a method of an object, its this is set to the object the method is called on. element
Simply speaking, this is pointing to one upper level of the element in most case.
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
What do you think of AMD vs CommonJS?
Explain why the following doesn’t work as an IIFE: function foo(){ }();.
Need a () to wrap the function body and declarations.
1 |
(function foo() {})() |
IIFE: An immediately-invoked function expression (or IIFE, pronounced “iffy”) is a JavaScript design pattern which produces a lexical scope using JavaScript’s function scoping.
What’s the difference between a variable that is: null, undefined or undeclared? How would you go about checking for any of these states?
undeclared:
A variable is undeclared when it does not use the var keyword. It gets created on the global object (that is, the window), thus it operates in a different space as the declared variables.
undefined:
Something is undefined when it hasn’t been defined yet. If you call a variable or function without having actually created it yet the parser will give you an not defined error.
null:
null is a variable that is defined to have a null value.
undeclared variables don’t even exist
null variables exist and have null assigned to them
First, use typeof to check the type of variable
Second, null == undefined is true, null === undefined is false