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.)

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.)

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();



Leave a Reply 11

Your email address will not be published. Required fields are marked *


Wojta

Wojta

Wrong, wrong, wrong

The second option makes no sense with Formatter. There is no object with such a name.

Object is called getFormatter!

Wojta

Wojta

Instead of formatter use the option with private property -> “var _private = function…”

M

M

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);
};
}

Anotnio

Anotnio

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.

Eyuel

Eyuel

that’s right M

jam

jam

This answer is right

Al

Al

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.

Al

Al

M’s answer is correct after all. I figured out what I was doing wrong. I needed to create a new instance of the getFormatter() function in order to access the parseValue property. You can see the implementation here http://jsfiddle.net/tbfzfg94/

XXX

XXX

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

Sohaib

Sohaib

function getFormatter() {

var _private = function (data) {
return custom(data);
};
return { parseValue : function (input) {
return _private(input);}
}

}

ANY

ANY

This is the right one