Skip navigation


1) Consider the code:
var someFunc = function() {
var a;
this.b;
c;
}

What are the scopes of variables a, b and c respectively?

Pick one of the choices
a) Private, Public, Global
b) Public, Static, Private
c) Global, Public, Static
d) Static, Public, Global

****************

2) Which of the following is NOT a benefit of using anonymous functions?

Pick one of the choices
a) The global space remains unpolluted
b) Anonymous functions can be self executing
c) Anonymous functions can be used to pass self references
d) Anonymous functions can be passed as callbacks

*********************

3) Inside a javascript function, which of the following will return a reference to the function itself?

Pick one of the choices
a) arguments
b) arguments.callee
c) arguments.callee.caller
d) this

*******************

4) Inside a javascript function, which of the following will return a reference to the calling function?

Pick one of the choices
a) this.caller
b) arguments.caller
c) this.callee.caller
d) arguments.callee.caller

***************

5) Which of the following is true about null ?

Pick one of the choices
a) It can implicitly be assigned by the JS environment to a variable
b) It is the same as undefined since null==undefined returns true
c) It is truthy in nature
d) The type of null is object

*************

6) Which of these is not a falsy value?

Pick one of the choices
a) “” (empty string)
b) [] (empty array)
c) null
d) undefined

**************

7) Consider the code :

var func = (function() {
var a = ‘Hello World’;
var b = function() {
return a;
}
return b;
})() ;

This is an example of ?

Pick one of the choices
a) Method Overloading
b) Closure
c) Closure and Anonymous Functions
d) Closure, Anonymous Functions and Self Executing Functions

***************

8) Consider the following statements:

a) An inner function has access to the variables of its parent function
b) A parent function has access to the variables of its inner functions
c) Variables declared inside a function are accessible outside of it as well
d) Variables declared outside any functions are accessible globally

Which of the above statements are true?

Pick one of the choices
a) I only
b) II only
c) I and III only
d) I and IV only

********************

9) When an object is assigned to a variable using the assignment (=) operator, what really happens?

Pick one of the choices
a) The object is copied to the new variable
b) The object’s reference is copied to the new variable
c) There is a compile error since objects cannot be copied
d) There is a run time error since objects cannot be copied

********************

10) The prototype is :

Pick one of the choices
a) A property of each object
b) A property of each function
c) A method of each object
d) A method of each function

****************

11) For any given function func , func.prototype.constructor will return?

Pick one of the choices
a) A reference to the function itself
b) A reference to the calling function
c) The function will call itself
d) A reference to the prototype object

*****************

12) For any given object obj, obj.constructor.prototype will return?

Pick one of the choices
a) Undefined
b) Error because o.constructor is not defined
c) The prototype of the object’s constructor function
d) The object’s constructor function

*******************

13) The type of prototype is?

Pick one of the choices
a) Function
b) String
c) Object
d) Array

*******************

14) To replicate an object exactly, you need to:

Pick one of the choices
a) Assign the object to a new variable
b) Extend the object with shallow copy
c) Extend the object with deep copy
d) Objects cannot be replicated. Only their references can be reassigned

*******************

15) Consider the code:

function foo() {
“use strict”;
// … your code …
}

This will:

Pick one of the choices
a) Throw a compile error as a string literal has been left unassigned
b) Will ignore the unassigned string literal and move on
c) Throw a warning stating that an unassigned string literal was found
d) Start throwing errors for potentially unsafe actions inside the function


Problem:
JavaScript can be loaded dynamically on the fly to improve the performance of the page. Basically it is used as lazy loading without blocking other important content of the page.

Here is the code snippet

//
//
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", "myscript.js")
if (typeof fileref!="undefined") document.getElementsByTagName("head")[0].appendChild(fileref)
//
//

One issue with this approach is that you can’t see myscript.js in chrome’s sources tab if you want to debug.

Solution:
Place a comment at the end of the script like below
//@ sourceURL=myscript.js

Reference:
https://developers.google.com/chrome-developer-tools/docs/javascript-debugging#source-maps

Hope this helps.


Everything in javascript is object. It a powerful feature. So Array is object. Stay focused.

// straight forward object, property creation & storing, retrieving value 
var obj = {}; // equivalent of new Object()
obj.age = 22; // create a property age and store value in it
console.log(obj.age); // 22
console.log(obj['age']); // 22
//

Now comes the interesting part with array. Still remembering Array is an object??

//
var arr = [];  // equivalent of new Array()
arr[0] = 'one';  // adds to the array
arr[1] = 'two';  // adds to the array
arr['age'] = 22; // does not add to the array but with property above
console.log(arr.length);  // 2 not 3
// gotcha
for(me in arr) {
  // still you will get 'age' here  !!!!
}
//

Hold on, we are going to explore Array like objects now. Array like objects have length property, numbered elements. Yes, it stops there. It does not have push(), pop(), sort(), splice(), etc. ie. any of Array functions. You can’t even loop through it. Perfect example for these kind of objects are HTML node sets like document.forms, document.getElementByTagName(), etc. Even arguments object also belongs to this category. Now let us jump into code to prove these points.

//
console.log(document.forms.length);  // 1 if you have one form element
document.forms[0]; // return first form in the document
document.forms.join(', '); // throws error !!!
// another example
function arrayLike(a,b) {
  alert(arguments.length);  // No error. works fine. arguments is a property created automatically with every function
  for(var i=0;i<arguments.length;i++) {
    alert("parameters "+ i + " is " + arguments[i]);  // works fine
  }
  alert("your parameters were " + arguments.join(", "));  // throws error
}
//

Now comes interesting part. How to convert array like object to get benefits of array functions??

//
function behave(a,b){
  var para = Array.prototype.slice.call(arguments);  // this needs explanation. read below
  alert(" arguments " + para.join(", "));   // works fine
}
//

Read note in the specification document.

Array: original object from where all array inherit their functions like join, pop, etc.
Array.prototype: this gives access to all functions in Array object
Array.prototype.slice: the native method; take this as Array but not arguments
Array.prototype.slice.call: call is prototype methods of Function which available to all functions; call changes the this context to the first argument passed

Wow!! Finally slice will return array object though whatever we pass in.

Bit lengthy post this time. Feel free to shoot your questions in the comment section.

Another proof.

Follow

Get every new post delivered to your Inbox.

Join 150 other followers