I am developing a website. I have come to a conclusion that I need to build a somewhat more complex framework for my javascript code. I have started researching different example of how to write a javascript library. I came up with somewhat confusing ways to write down methods. I'd really appreciator some clarification.
So here is how I would normally write down a method:
var ReportEnhancements =
function () {
this.Name = function()
{
alert('It is me!');
}
}
And here is another way to represent methods:
ReportEnhancements.prototype.Tooltip = {
setByTitle :function(elementsToTooltip) {
alert('I am about to be tooltiped!')
}
}
There are two things that puzzle me:
- When should I use the prototype keyword? When should I just declare the method into a member's name?
- Look at setByTitle and Name, as you can see, they are different, even though they both declare methods and both methods are public.
- Why the difference between this.Name = function() and setByTitle :function() . Why is there two different syntaxes to declare methods?
Edit:
I am getting closer to understanding the differences, but there is one big issues I haven't completely grasped yet. Why do these two ways to represent methods, and thereby classes have two different access rules. I cannot seem to declare a private method in a javascript object literal. On the other hand, if I have got a regular nested element, I cannot seem to expose it as a public modifier. What is the thing with the access modifiers ?
Thank you!