2016-08-30 65 views
0

我想做一個函數,返回一些代碼,我正在努力這樣做。Javascript返回函數的語法

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

這就是我現在所擁有的,但它不工作。我需要類似...

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

這只是一個語法的東西?或者,我試圖做的只是根本上有缺陷?謝謝!

+1

你幾乎有它。而不是'function getpresent(place)= {'try'function getpresent(place){return {'並用'}結束它' – Shanimal

回答

3

如果你喜歡返回object,那麼這將工作

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

您在這裏有很多的混合語法。

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

請注意,沒有轉譯器或瀏覽器支持ES6箭頭功能,這將無法正常工作。我不知道你要去哪個方向。

數組([ ])不能像代碼底部那樣包含鍵/值對。只有對象具有鍵/值對({ })。

另外,RETURN無效,您必須使用return才能從函數返回。

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

或與ES6語法:

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

取出=

正確的函數語法:

function myFunction(param) { 
    return param; 
}