Javascript 带有返回值的函数的数组
我有以下常数:Javascript 带有返回值的函数的数组,javascript,Javascript,我有以下常数: const getActionsList = { listActions: [ '$http', '$route', '$q', '$location', function($http, $route, $q, $location) { const id = $route.current.params.id; const deferred = $q.defer();
const getActionsList = {
listActions: [
'$http',
'$route',
'$q',
'$location',
function($http, $route, $q, $location) {
const id = $route.current.params.id;
const deferred = $q.defer();
$http.get('/test/' + id).then(
function(result) {
if (result.data.length > 0) {
deferred.resolve(result.data);
} else {
var urlRedirect = '/';
$location.url(urlRedirect).replace();
}
},
function(result) {
deferred.reject(result);
}
);
return deferred.promise;
}
]
};
getActionsList中的函数必须返回一个值
我想当我在另一个函数中调用它时,它会返回一个值,而不是一个函数
var menu = function() {
return {
mediaType: function() {
return getActionsList[4];//should return a value rather than a function
}
};
};
如何让getActionsList[4]返回值?只需让mediaType使用函数而不是返回函数:
var menu = function() {
return {
mediaType: getActionsList[4];//should return a value rather than a function
};
};
可能是这样
return getActionsList.listActions[4]()
但需要传递参数
getActionsList[4]
将返回一个数组值,它是一个函数。
您需要调用函数,以获得结果:)
试试我的代码:
var menu = function() {
return {
mediaType: (function() {
return getActionsList.listActions[4](); //should return a value rather than a function
})()
};
};
get ActionsList是一个对象,在该对象中放置一个listActions数组
现在在函数表达式菜单中,您希望返回一个mediaType,它应该是一个匿名函数。您需要调用函数来返回值。您还需要调用listActions[4]。所以你需要用这种方式来表达listActions4。由于listAction位于对象getActionsList中,因此它现在将成为getActionsList.listActions4
var menu = function() {
return {
mediaType: (function() {
return getActionsList.listActions[4](); //should return a value rather than a function
})()
};
};