There currently is no meaning for the keyword public in standard JavaScript.
Based on your original code snippet, I suspect you meant:
function myObject(){
this.keyOne=1;
this.keyTwo=2;
this.keyThree=3;
function add(){
return this.keyOne+this.keyTwo+this.keyThree;
}
return {
add: add
};
}
The function would then return an object that only has one property: the add function. That object is created by the object literal after the return keyword.
But then there is no point using this. You could have written:
function myObject() {
var keyOne=1;
var keyTwo=2;
var keyThree=3;
function add() {
return keyOne + keyTwo + keyThree;
}
return {
add: add
};
}
Or even more succinctly:
function myObject() {
var keyOne=1;
var keyTwo=2;
var keyThree=3;
return {
add: function() {
return keyOne + keyTwo + keyThree;
}
};
}
This has the added advantage that you don't need to call it prefixed with new. It's just an ordinary function that creates and returns an object which contains another function:
var o = myObject();
alert(o.add());
You could allow the caller to specify the numbers to be added, like this:
function myObject(keyOne, keyTwo, keyThree) {
return {
add: function() {
return keyOne + keyTwo + keyThree;
}
};
}
var o = myObject(5, 4, 7);
alert(o.add());