I am developing a website, I came to a conclusion that to create a more complex structure for my Javascript code is required. I have started researching various examples of writing a JavaScript library. I came in some confusing way to write methods. I really appreciate some explanations.
So here I am going to normally write a method:
var ReportEnhancements = function () {this.Name = function () {alert ('this I have it! '); }} And here's another way to represent methods:
ReportsExperience. Protopp TollTip = {setByTitle: function (elementToTooltip) {Warning ('I'm going to be tooltip!')}} There are two things that make me puzzle:
- When should I use prototype keywords? When should I declare the same method in the name of a member?
- Look at setByTitle and name, as you can see, they are different, even if they declare both methods and both methods are public
- between Why is the difference.Name = function () and setByTitle: function () Why are there two different syntax to declare methods?
Edit: I am getting closer to understanding the difference, but one big issue I have not yet fully understood is. Why are these two ways to represent methods, and thus classes have two separate access rules. I can not declare a private method literally in a javascript object. On the other hand, if I have found a nested element on a regular basis, I may not have to show it as a public modifier. What's with the access modifier?
Thank you!
An important difference (although the answer is no why either ) Is the scope. In the past, using this.Name () is any other variable declared in the external function, because it is all closed, it is known as the "privileged method" . The latter version, which is completely out of the original frequency, does not have access to those "private" member variables.
Technically the latter is really more memory efficient, because that function is created only once the internal function in the first version is created every time that type of object is created in an instant. If you have only some objects that are around it, then it does not matter, but if you are going to create hundreds of them then you should actually use the prototype based method. Edit - Regarding your edit about access modifiers: OK, actually any access amendment Not at all. The former method creates a "closing", and there may be variable announcements in the closure, which has local-only scope. However, a function is also an object, so by adding properties on this , those properties are exposed in the outside world, a common method of "exporting" a method or variable is to do this: var MyType = function () {var method = function () {...}; // This private variable is this.method = method; // This is a public property;}; // In the personal variable On the other hand, the latter method does not stop with an object, it is simply a heap of the name property whose value function references To include. All properties of an object can be accessed from the outside, so the thing created with an object is "Public".
Comments
Post a Comment