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.)
Select and Place:

PrepAway - Latest Free Exam Questions & Answers

Answer:

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

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


Leave a Reply