How to write functions


#1



https://www.codecademy.com/en/courses/close-the-super-makert/0/5?curriculum_id=506324b3a7dffd00020bf661


My code works but I have a question about the different ways to write a function.


voidLastTransaction: function () {

This code only worked when I wrote the function like you see above, yet in other exercises I have see a function written like:

var voidLastTransaction = function() {

or

function voidLastTransaction () {

How come these two forms of function don't work in this problem and when should one use these other forms of writing a function statement?


#2

@jrenai,

voidLastTransaction: function () {}

In cashRegister object
you defined a couple of properties separated from eachother by comma ,

Each property consisted of a property-key and its associated Value
For instance you had
a total property with property-key total** and its associated number Value 0
you also had to create
a voidLastTransaction property with property-key voidLastTransaction and its associated function Value _function () { }

As the property voidLastTransaction has an associated function-Value

voidLastTransaction is called a METHOD

cashRegister.voidLastTransaction();

============================================================

var voidLastTransaction = function() {};

( take notice:: you end the definition with a semi-colon ; )

Here you define a variable voidLastTransaction
to which you assign a so-called anonymous function

To call this anonymous function
you will have to append a pair-of-parentheses-() to the variable name
like

voidLastTransaction();

=============================================================

function voidLastTransaction () {}

voidLastTransaction ()

http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1