前端工程师面试题总结 - JavaScript(1)

来源:互联网 发布:h网络是什么意思 编辑:程序博客网 时间:2024/05/19 06:17

1. Describe closure

Simply accessing variables outside of your immediate lexical scope creates a closure.

For example:

var test = function () {

    var count = 0;

    var addOne = function(){ count += 1;}

    return addOne;

}

var t = test();    // here t refers to the addOne function, and since addOne refers to the test object, the whole test object will be stored in the RAM

t();  // now the count equals to 1

2. What is event bubbling

Event bubbling and capturing are two ways of event propagation in HTML DOM.

In bubbling the event is first captured and handled by the inner most element and then propagated to outer elements.

In capturing the event is first captured by the outer most element and propagated to the inner most element.

Both are part of the W3C standard. As per the standard first the event will capture it till it reaches the target then it will bubble up to the outer most element.

We can use the addEventListener(type, listener, useCapture) to register event handlers for bubbling and capturing phases. To use the capturing phase event handling pass the third argument as true.

IE uses only event bubbling where as firefox supports both bubbling and capturing.

Only event bubbling model is supported by all the major browsers.So if you are going to use event capturing still you need to handle event bubbling for IE. So it will easier to use event bubbling instead of capturing.

P.S: other questions like have you used Angular.js, node.js, or any other similar libraries. Learn as much as you can.

0 0