user5826447
user5826447

Reputation: 367

Javascript return function syntax

I would like to make a function that returns some code and am struggling to do so.

function getpresent (place) = {
  type: "single-stim",
  stimulus: getword(place),
  is_html: true,
  timing_stim: 250,
  timing_response: 2000,
  response_ends_trial: false,
  };

This is what I have right now, but it is not working. I need something like...

function getpresent (place) = {
   RETURN [
  type: "single-stim",
  stimulus: getword(place),
  is_html: true,
  timing_stim: 250,
  timing_response: 2000,
  response_ends_trial: false,
],  
};

Is this just a syntax thing? Or is what I'm trying to do just fundamentally flawed? Thanks!

Upvotes: 0

Views: 65

Answers (4)

Nina Scholz
Nina Scholz

Reputation: 386570

If you like to return an object, then this would work

function getpresent (place) {
    return {
        type: "single-stim",
        stimulus: getword(place),
        is_html: true,
        timing_stim: 250,
        timing_response: 2000,
        response_ends_trial: false
    };
}

Upvotes: 3

Loay
Loay

Reputation: 226

Remove the =

proper function syntax:

function myFunction(param) {
    return param;  
}

Upvotes: 0

Pavlin
Pavlin

Reputation: 5528

function getpresent(place) {
  return {
    type: "single-stim",
    stimulus: getword(place),
    is_html: true,
    timing_stim: 250,
    timing_response: 2000,
    response_ends_trial: false,
  }
}

or with the ES6 syntax:

const getpresent = (place) => ({
  type: "single-stim",
  stimulus: getword(place),
  is_html: true,
  timing_stim: 250,
  timing_response: 2000,
  response_ends_trial: false,
});

Upvotes: 1

adam-beck
adam-beck

Reputation: 6009

You have a lot of mixed syntax here.

var getpresent = place => ({
  type: 'single-stim',
  stimulus: getword(place),
  is_html: true,
  timing_stim: 250,
  timing_response: 2000,
  response_ends_trial: false
});

Note, this will not work without a transpiler or with a browser that supports ES6 arrow functions. I didn't know which direction you were heading.

An array ([ ]) cannot contain Key/Value pairs like you have in the bottom section of code. Only objects have Key/Value Pairs ({ }).

Also, RETURN is not valid and you must use return in order to return from a function.

Upvotes: 1

Related Questions