Let's look on this JavaScript code:
Person: function() {
var element = null;
this.init = function() {
// select an HTML element created+ added to DOM in init()
element = $("#justCreatedElement");
};
this.workingFunction = function() {
// use private member `element`
element;
// create new variable and select again with jquery
var element = $("#justCreatedElement");
}
}
We can assume that the init()
method gets called before any other method.
I have the following Question:
1 In the init()
I use JQuery to assign a HTML element to the private member element
. Because element
is visible for all methods I can just use it again in the method workingFunction2()
. What is better coding standard:
I Using element
private member in all member mehtods and even maybe write it in any member methof.
II Using element
private member only to read in all member methods, except once, where it is set for the first time, here in init()
. And if private member element
must be changed use a mehtod called changeELement()
but do not write it in a method where the user cannot know that it get written to.
III General avoid to use a member variable that holds JQuery selections and always select again in every mehtod
Or can somebody give me his own standards regarding this problem.