PrepAway - Latest Free Exam Questions & Answers

How should you complete the relevant code?

DRAG DROP
You are developing a shared library to format information. The library contains a method named _private.
The _private method must never be called directly from outside of the shared library.
You need to implement an API for the shared library.
How should you complete the relevant code? (Develop the solution by selecting the required code
segments and arranging them in the correct order. You may not need all of the code segments.)

PrepAway - Latest Free Exam Questions & Answers

Answer: See the explanation.

Explanation:
Box 1:

Box 2:

Box 3:

Box 4:

Note:

* Here there is a basic example:

// our constructor
function Person(name, age){
this.name = name;
this.age = age;
};
// prototype assignment
Person.prototype = (function(){
// we have a scope for private stuff
// created once and not for every instance
function toString(){
return this.name + " is " + this.age;
};
// create the prototype and return them
return {
// never forget the constructor ...
constructor:Person,
// "magic" toString method
toString:function(){
// call private toString method
return toString.call(this);
}
};
})();

* Example:
You can simulate private methods like this:

function Restaurant() {
}
Restaurant.prototype = (function() {
var private_stuff = function() {
// Private code here

};
return {
constructor:Restaurant,
use_restroom:function() {
private_stuff();
}
};
})();
var r = new Restaurant();
// This will work:
r.use_restroom();
// This will cause an error:
r.private_stuff();

11 Comments on “How should you complete the relevant code?

  1. M says:

    I’ve been searching for the correct answer all over internet of this question and the most relevant answer I found is the following:

    function getFormatter() {
    var _private = function (data) {
    return custom(data);
    };

    this.parseValue = function (input) {
    return _private(input);
    };
    }




    3



    2
    1. Anotnio says:

      I think M is right.

      Following the pattern used to build a variable as a private one (i.e. http://javascript.crockford.com/private.html), you should use the “var” javascript word inside the constructor.

      So putting:

      var _private = function(data){…}

      you assure that the method _private is not a public one, directly uncallable from outside.

      The ony method directly callable from outside is the parseValue, because of the use of the “this” javascrip word:

      this.parseValue = function(input){
      return _private(input);
      };

      Inside this public method the _private method is invoked, permitting the getFormatter object to provide to outside callers the incapsulated result of _private method.




      0



      0
      1. Al says:

        There is no truly correct answer. The 8th block is meaningless inside the getFormatter() function and calling getFormatter().parseValue([somevalue]) will throw an error “Unable to get property ‘parseValue’ of undefined or null reference.” The 5th block is closer because the parseValue() function is returned as a parameter to the getFormatter() function. However, the call to _private() within the parseValue() function does not pass on the input argument and therefore the data argument in the _private() function will always be null.




        0



        0
          1. XXX says:

            you can just use block 5 instead of block 8 and will work without the “new” keyword… which is in my opinion a correct answer. But who knows as it can also work with new keyword as in your fiddle




            0



            0

Leave a Reply