简体   繁体   中英

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!

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

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.

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

Remove the =

proper function syntax:

function myFunction(param) {
    return param;  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM