{"id":520,"date":"2014-10-06T12:46:37","date_gmt":"2014-10-06T20:46:37","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=520"},"modified":"2019-05-23T14:08:42","modified_gmt":"2019-05-23T22:08:42","slug":"javascript-set2-of-interview-questions","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=520","title":{"rendered":"Javascript Set2 of Interview questions"},"content":{"rendered":"<p><strong>1) Does Javascript support read only attributes of Object? <\/strong><\/p>\n<p>With any javascript interpreter that implements ECMAScript 5 you can use Object.defineProperty to define readonly properties. In loose mode the interpreter will ignore a write on the property, in strict mode it will throw an exception.<\/p>\n<pre>var obj = {};\r\nObject.defineProperty( obj, \"\", {\r\n  value: \"\",\r\n  writable: false,\r\n  enumerable: true,\r\n  configurable: true\r\n});\r\n<\/pre>\n<p>Also you can try to attempt this via private attributes simulation.<\/p>\n<p><strong>2) What does the delete operator do ? <\/strong><br \/>\ndelete is used to delete object properties. It will not delete ordinary variables defined with var. it will delete global variables though since they are actually properties of the global object.<\/p>\n<p><strong>3) What are all the types of Pop up boxes available in JavaScript?<\/strong><\/p>\n<p>Alert<br \/>\nConfirm and<br \/>\nPrompt<\/p>\n<p><strong>4) What does the void operator do<\/strong><br \/>\nThe void operator is often used merely to obtain the undefined primitive value, usually using &#8220;void(0)&#8221; (which is equivalent to &#8220;void 0&#8221;). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value). undefined isn&#8217;t a JavaScript keyword, but a property of the global object. So it can be shawdowed sometimes. http:\/\/adripofjavascript.com\/blog\/drips\/javascripts-void-operator.html<\/p>\n<p><strong>5) What does use strict in javascript do ? What kind of errors does it catch ? <\/strong><br \/>\nIf you use &#8220;use strict&#8221; javascript will flag error for certain cases.<\/p>\n<p>x = 3.14; \/\/ will cause error since x is not declared<br \/>\nx = {p1:10, p2:20}; \/\/ using object without declaring it<br \/>\nvar x = 3.14; delete x; \/\/ deleting a variable will cause an error<br \/>\nfunction x(p1, p2) {}; delete x; \/\/ deleting a function will cause an error<br \/>\nfunction x(p1, p1) {}; \/\/ Duplicating parameter names will cause an error<\/p>\n<p><strong>6) What is event bubbling and event capturing ?<\/strong><br \/>\nEvent bubbling and capturing are two ways of event propagation in the HTML DOM API, when an event occurs in an element inside another element, and both elements have registered a handle for that event. The event propagation mode determines in which order the elements receive the event.<\/p>\n<p>With bubbling, the event is first captured and handled by the innermost element and then propagated to outer elements.<\/p>\n<p>With capturing, the event is first captured by the outermost element and propagated to the inner elements.<\/p>\n<p>We can use the addEventListener(type, listener, useCapture) to register event handlers for in either bubbling (default) or capturing mode. To use the capturing model pass the third argument as true<\/p>\n<p>The event handling process:<\/p>\n<ul>\n<li>When an event happens \u2013 the most nested element where it happens gets labeled as the \u201ctarget element\u201d (<code>event.target<\/code>).<\/li>\n<li>Then the event first moves from the document root down to the <code>event.target<\/code>, calling handlers assigned with <code>addEventListener(...., true)<\/code> on the way (<code>true<\/code> is a shorthand for <code>{capture: true}<\/code>).<\/li>\n<li>Then the event moves from <code>event.target<\/code> up to the root, calling handlers assigned using <code>on&lt;event&gt;<\/code> and <code>addEventListener<\/code> without the 3rd argument or with the 3rd argument <code>false<\/code>.<\/li>\n<\/ul>\n<p>Each handler can access <code>event<\/code> object properties:<\/p>\n<ul>\n<li><code>event.target<\/code> \u2013 the deepest element that originated the event.<\/li>\n<li><code>event.currentTarget<\/code> (=<code>this<\/code>) \u2013 the current element that handles the event (the one that has the handler on it)<\/li>\n<li><code>event.eventPhase<\/code> \u2013 the current phase (capturing=1, bubbling=3).<\/li>\n<\/ul>\n<p>Any event handler can stop the event by calling <code>event.stopPropagation()<\/code>, but that\u2019s not recommended, because we can\u2019t really be sure we won\u2019t need it above, maybe for completely different things.<\/p>\n<p>The capturing phase is used very rarely, usually we handle events on bubbling. And there\u2019s a logic behind that.<\/p>\n<p>In real world, when an accident happens, local authorities react first. They know best the area where it happened. Then higher-level authorities if needed.<\/p>\n<p>The same for event handlers. The code that set the handler on a particular element knows maximum details about the element and what it does. A handler on a particular <code>&lt;td&gt;<\/code> may be suited for that exactly <code>&lt;td&gt;<\/code>, it knows everything about it, so it should get the chance first. Then its immediate parent also knows about the context, but a little bit less, and so on till the very top element that handles general concepts and runs the last.<\/p>\n<p>Bubbling and capturing lay the foundation for \u201cevent delegation\u201d \u2013 an extremely powerful event handling pattern<\/p>\n<p><strong>7) What is hoisting in javascript ? <\/strong><br \/>\nHoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. Inevitably, this means that no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local.<\/p>\n<p>Of note however, is the fact that the hoisting mechanism only moves the declaration. The assignments are left in place.<\/p>\n<p><strong>8) Make this work <\/strong><br \/>\nduplicate([1,2,3,4,5]); \/\/ [1,2,3,4,5,1,2,3,4,5]<\/p>\n<pre>var newarr = duplicate([1,2,3,4,5]); \/\/ [1,2,3,4,5,1,2,3,4,5]\r\n\r\nfunction duplicate(arr) {\r\n   return arr.concat(arr);\r\n}\r\n<\/pre>\n<p><strong>9) What\u2019s the difference between feature detection, feature inference, and using the UA string?<\/strong><br \/>\nWhen you check if a certain feature exists, that\u2019s feature detection.<br \/>\nif(typeof(Text) === &#8220;function&#8221;){<\/p>\n<p>When you make an assumption that because one feature is present (or not) another one will also be present (or not)<br \/>\nif(typeof applyElement != &#8216;undefined&#8217;) {<br \/>\n\/\/ now I know I&#8217;m not in IE, I&#8217;ll just assume Text is available<br \/>\ntext = new Text(&#8216;Oh, how quick that fox was!&#8217;);<\/p>\n<p>\u201cUA\u201d stands for user agent, which means the browser (and a whole lot of other stuff). Just like with feature inference, if you use the UA string you&#8217;re making an assumption about how the string will be written, what changes are likely to happen in this particular version. Using UA string to infer things is also bad.<\/p>\n<p><strong>10) What does the javascript comma operator do ? <\/strong><br \/>\nThe comma operator evaluates both of its operands (from left to right) and returns the value of the second operand. (MDC)<\/p>\n<pre>var a = (7, 5);\r\na; \/\/5\r\n \r\nvar x, y, z\r\nx = (y=1, z=4);\r\nx; \/\/4\r\ny; \/\/1\r\nz; \/\/4\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1) Does Javascript support read only attributes of Object? With any javascript interpreter that implements ECMAScript 5 you can use Object.defineProperty to define readonly properties. In loose mode the interpreter will ignore a write on the property, in strict mode it will throw an exception. var obj = {}; Object.defineProperty( obj, &#8220;&#8221;, { value: &#8220;&#8221;, &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=520\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Javascript Set2 of Interview questions<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-520","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=520"}],"version-history":[{"count":14,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/520\/revisions"}],"predecessor-version":[{"id":1033,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/520\/revisions\/1033"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=520"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}