I am learning to code Javascript OOP style and I have a few questions, mostly about declaring variables and not using global ones.
Question 1 - am I doing the following coding correctly?
Here i declare the "Fields" class:
/**
* Display Fields
*/
var Fields = function(){
this.display = function(fields){
var test = '1st method of declaring test';
this.test = '2nd method of declaring test';
for (var i = 0, len = fields.length; i < len; i++) {
jQuery('[name=' + fields[i] + ']').closest('.row').css('display', 'block');
}
};
}
Now I instantiate the class Fields, creating the object fields
// Fields Object
var fields = new Fields();
fields.display(requiredFields.concat(normalFields));
Question 2 - Which is the correct way of declaring class variables - in this case: "test":
this.test = 'value'
or just:
var test = 'value'
Can you explain why 1 method is preferred over the other?
Question 3 - is the "i" inside the for loop declared correctly? Is this the correct way of doing it? Or I need to be using something like:
this.i = 0
Any advice on good practices is strongly appreciated.
I read allot on the net but I am new to OOP altogether and I want to make sure I am on a correct path.
Ty!