前端面试问题集

  1. 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

  2. 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

  3. Explain how this works in JavaScript htm

    Function:

    this point to the function itself. ip

    Object:

    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

  4. What do you think of AMD vs CommonJS?

  5. 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.

  6. What’s the difference between a variable that is: nullundefined 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

    • undefined variables exist, but don’t have anything assigned to them
    • 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

  7. What's the difference between host objects and native objects?
相关文章
相关标签/搜索