Skip navigation

Tag Archives: private


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


Here is the code for creating scope inside if block.

function foo() {
  var x = 1;
  if (x) {
    (function () {
      var x = 2;
      // some other code
     }());
  }
  // x is still 1.
}

If it is strange to you, here are some clues

var foo = 1;
function bar() {
  if (!foo) {
    var foo = 10;
  }
  alert(foo);
}
bar();

If it surprises you that the answer is “10”? Here goes another

var a = 1;
function b() {
  a = 10;
  return;
  function a() {}
}
b();
alert(a);

is it terrified to see the browser alert “1”? here goes last one…

Try the following in Firebug:

var x = 1;
console.log(x); // 1
if (true) {
  var x = 2;
  console.log(x); // 2
}
console.log(x); // 2

This is because JavaScript has function-level scope.
One more concept is variable hoisting. The simplest example is

function foo() {
  bar();
  var x = 1;
}

is actually interpreted like this:

function foo() {
  var x;
  bar();
  x = 1;
}

I know, this post will not give you fair idea what Im talking about. But run the code in firebug, if you are curious, please dig on these keywords in google javascript hoisting, javascript scope.

The main purpose of this post is to invoke your curiosity. Happy JavaScripting !!!


function Man(name) {

// private property
var lover = “simron”;

// public property
this.wife = “rocy”;

// privileged method
this.getLover = function(){return lover};

// public method
Man.prototype.getWife = function(){return this.wife;};

}

// static property
Man.mother = “teresa”;

Now creating instance & checking access

// create a new instance
var emp = new Man(“raja”);

// access private property
emp.lover;   // undefined

// access public property
emp.wife;  // rocy

// access privileged method
emp.getLover();   // simron

// access public method
emp.getWife();  // rocy

// access static property
Man.mother;  // teresa

Need one more example??

var Female = (function() {
   var _trueAge = 48,
        _trueWeight = 125;

   return {
      age : _trueAge - 15,
      weight : _trueWeight - 30
   };
})();

Female.age; // 33
Female.weight; // 95
Female._trueAge; // undefined