Ans. Using global variables in Javascript is evil and a bad practice. That being said, namespacing is used to bundle up all your functionality using a unique name. In JavaScript, a namespace is really just an object that you’ve attached all further methods, properties and objects. It promotes modularity and code reuse in the application.
21. What is the difference between innerHTML and append() in JavaScript?
Ans. InnerHTML is not standard, and its a String. The DOM is not, and although innerHTML is faster and less verbose, its better to use the DOM methods like appendChild(), firstChild.nodeValue, etc to alter innerHTML content.
22. When does div.setAttribute(‘###’) not equal div.###?
Ans. When you’re trying to set an element’s class attribute, IE has issues. Therefore, it’s better to use .className instead of .setAttribute(). There is also an issue with .setAttribute() and style in IE, so .style should be used.
// Don't do this: el.setAttribute('class ', ' foobar '); // Do this instead: el.className = ' foobar '; // Don' t do this : el.setAttribute( 'style' , 'color: #000' ); // Do this instead: el.style.color = '#000' ; |
23. What’s the difference between these two statements:
var x = 3;
x = 3;
Ans. The first puts the variable in the scope of whatever function it was defined. The second places the variable in global scope. It can potentially cause collision with other variables with the same name. Therefore, the keyword var should always be used when defining variables, and an anonymous function should be used as a closure if need be, encapsulating multiple functions which can share access to the same set of variables. That makes sure the variables stay sandboxed, accessible only by those functions which need them.
24. What’s the difference between:
!!(obj1 && obj2);
(obj1 && obj2);
Ans. The first returns a “real” boolean value, because you first negate what is inside the parenthesis, but then immediately negate it again. So, it’s like saying something is “not not” truth-y, making it true. The second example simply checks for the existence of the obj1 and obj2, but might not necessarily return a “real” boolean value, instead returning something that is either truth-y or false-y. This can be problematic, because false-y can be the number 0, or an empty string, etc. Simple existence can be truth-y. A “real” boolean will only be true or false.
25. Write a one-line piece of JavaScript code that concatenates all strings passed into a function.
Ans.
function concatenate() { return String.prototype.concat.apply( '' , arguments); } |
26. What do these two examples have in common?
Example 1:
var obj = document.getElementById( 'adiv' ); document.getElementById( 'adiv' ).ptr = obj; |
function assignClick() { var el = document.createElement( 'div' ); function handleClick() { el.innerHTML = 'clicked!' ; } el.attachEvent( 'onclick' , handleClick); } |
2. (Example 1)This is bad practice, because you first assign a DOM element to a variable, but then you assign that same element to a (nonexistent) property of the element itself. This creates a sort of circular logic loop, and will negatively impact performance.
3. (Example 2)This example will only work in Internet Explorer, because it employs the proprietary .attachEvent() method. Granted, .innerHTML is also proprietary (now a standard), but unlike .attachEvent(), .innerHTML is widely supported in all modern browsers. To make this example more cross-browser friendly, the W3C standard .addEventListener() should attempted first, and if it does not exist, then try .attachEvent() as a fallback.
27. What is the result of a.foo(2)?
var a = ( function (y,x) { var x = null ; return { foo: function (x){ return this .bar(x * y); }, bar: function (x){ return x + y; } } })(3,4); |
1. function(3,4) // 4 is not used in this example, y becomes 3
2. foo(2) // x is passed in as param
3. return bar(2 * 3) // y is 3 from initialization
4. bar(6) // x is passed in as param again
5. return 6 + 3 // y is still 3 from init
28. Why ’7′ + 4 gives ’74′ and ’7′ – 4 gives 3?
Ans. In first one it takes “+” as concatenation operation and considering ’7′ as string it concatenates the two value. In second one since considering “-” as minus arithmatic operators converts string ’7′ into number and performs arithmatic operation and calculates the value.
29. How can JavaScript make a Web site easier to use? That is, are there certain JavaScript techniques that make it easier for people to use a Web site?
Ans. JavaScript’s greatest potential gift to a Web site is that scripts can make the page more immediately interactive, that is, interactive without having to submit every little thing to the server for a server program to re-render the page and send it back to the client. For example, consider a top-level navigation panel that has, say, six primary image map links into subsections of the Web site. With only a little bit of scripting, each map area can be instructed to pop up a more detailed list of links to the contents within a subsection whenever the user rolls the cursor atop a map area. With the help of that popup list of links, the user with a scriptable browser can bypass one intermediate menu page. The user without a scriptable browser (or who has disabled JavaScript) will have to drill down through a more traditional and time-consuming path to the desired content.
30. What’s a way to append a value to an array?
Ans.
arr[arr.length] = value; |
31. How do you assign object properties?
Ans.
obj[ "age" ] = 17 or obj.age = 17. |
32. What is two different ways to create a new object in JavaScript?
Ans.
var obj = new Object(); or var obj = {}; |
33. What does the delete operator do?
Ans. The delete operator is used to delete all the variables and objects used in the program ,but it does not delete variables declared with var keyword.
34. Does javascript have the concept level scope?
Ans. No. Javascript does not have block level scope,all the variables declared inside a function possess the same level of scope unlike c,c++,java.
35. What is variable typing in javascript?
Ans. It is perfectly legal to assign a number to a variable and then assign a string to the same variable as follows example
i = 10; i = "string" ; |
36. What are undefined and undeclared variables?
Ans. Undeclared variables are those that are not declared in the program (do not exist at all),trying to read their values gives runtime error.But if undeclared variables are assigned then implicit declaration is done. Undefined variables are those that are not assigned any value but are declared in the program.Trying to read such variables gives special value called undefined value.
No comments:
Post a Comment