0

The jQuery framework has a lot of functions which will either retrieve or mutate values depending on the parameters passed:

$(this).html();       // get the html
$(this).html('blah'); // set the html

Is there a standard name for functions which behave like this?

nickf
  • 537,072
  • 198
  • 649
  • 721

3 Answers3

1

AFAIK, there is no "standard" name for a single function that acts both as a getter and as a setter. It's an idiom not very commonly used, but it's possible in some languages, and as long as it's well documented, there's no harm in using it.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • wouldn't it be possible in any language which allows either optional parameters (php, javascript, python, ruby ...), or function overloading (java...)? – nickf Apr 09 '10 at 09:23
  • Yes, as long as these languages have a way of distinguishing between "no parameter passed at all" and "parameter passed, but given the default value" – Eli Bendersky Apr 09 '10 at 11:05
1

Some call it a property.

mkj
  • 2,761
  • 5
  • 24
  • 28
0

Is there a standard name for functions which behave like this?

Getters and Setters.

EDIT:

I just realized that you want a single function to do this. The technical term for this is a bad idea. Unless your language provides properties, trying to rig up a single function to do this is very unreadable.

Rob Lachlan
  • 14,289
  • 5
  • 49
  • 99