Javascript Naming Convention: Value() vs getValue() [Closed]
Imagine a Simple Object: Function createObj(someValue) { Return { someFunction: Function () { Return someValue; } }; }; It Basically Does: Var Obj =...
Imagine a simple object:
function createObj(someValue) {
return {
someFunction: function () {
return someValue;
}
};
};
It basically does:
var obj = createObj("something");
var result = obj.someFunction(); // "something"
Now, someFunction refers to a function that returns a value.
What should be the correct naming convention used in javascript for the someFunction function?
Should it be named as what it does? Or its ok to name it with the name of the object that it returns?
I mean, should I name it value(), or should I name it getValue()? Why?
Thanks in advance.
4 Answers
There's no "correct" naming in JavaScript. However, there are three conventions that are mostly used:
Must Read
Independent getter and setter:
In this approach, we create both a getter and a setter functions, like in Java:
var value;
this.getValue = function () {
return value;
}
this.setValue(val) {
value = val;
}