1

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.

lante
  • 7,192
  • 4
  • 37
  • 57
  • 1
    I feel that you are asking an opinion based question, which means it should be flagged and closed, BUT as a note: Either follow company conventions or a format that you easily understand. `getValue` is easy enough, unless you want to overload `value` such that it will get if there is not an arg to set... – Fallenreaper Jul 16 '14 at 18:47
  • getValue() is a poor name because it doesn't describe what the function does since almost every function gets a value. it should be something more like getBalance() or getUser() – dandavis Jul 16 '14 at 19:29
  • the name **value** was just for example, of course I wouldn´t use that name for every function. If it returns a cow, it could be `cow()` or `getCow()` – lante Jul 16 '14 at 19:34
  • Once again the meta-trolls, driven by the gamification of Q&A on SO have shut down a perfectly good question. Of course there is "opinion". There are excellent, valid reasons why I want to read the answers to a question like this. Most importantly, to find out what people consider "best practices". One's code can be vastly improved based on such wisdom. – bearvarine Aug 31 '18 at 23:17

4 Answers4

4

There's no "correct" naming in JavaScript. However, there are three conventions that are mostly used:

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;
}

Combined getter/setter method

In this approach, you have a single method that does both. The magic here is that it checks whether you provides a new value for your property:

var _value;

this.value = function (val) {
    if (arguments.length > 0) {
        // Acts like a setter
        _value = value;
        return this;
    }
    return _value;
}

It's also common to define your setter to return the actual instance of your class, so you can do this:

myObj
     .setProp1("foo")
     .setProp2("bar");

Object.defineProperty

This one is less used, but it's also an option. It's similar to Objective-C @property statement:

var _value;
Object.defineProperty(this, "value", {
    get: function() {
        return _value;
    },
    set: function(val) { 
        _value = val;
    }
});

Then you can access it like if it was a public property:

console.log(myObj.value);  // Calls the getter method
myObj.value = newValue;    // Calls the setter method
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
1

General naming conventions say getValue() would be the name of a function, where value would be the name of the variable.

So, if you were accessing the data directly, you would use obj.value, whereas if you were using a function to get some data, you would use obj.getValue().

Cereal
  • 3,699
  • 2
  • 23
  • 36
  • There's plenty of production JavaScript code out there, including code in many libraries, that strongly disagrees (see jQuery for example). Doesn't mean it's *right*, but this is really a matter of opinion. – Pointy Jul 16 '14 at 18:48
1

There are quite a few naming conventions out there for Javascript in particular.

This question has a sister and hopefully this will help with your question: Clicky for more info

Community
  • 1
  • 1
Rizowski
  • 3,528
  • 5
  • 22
  • 31
1

I am personally a fan of getValue and setValue but when reading other peoples codebase, i have seen

object.value = function(item){
  if (!item) return this.value;
  this.value = item;
}

jQuery uses this on the regular, which is why i don't want to necessarily bash on it.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129