猿问

无法为访问函数创建 javaScript 算法

我的应用程序中有一些选项卡,如报告、任务等......此外,用户具有不同的权限,如reports.add、tasks.delete。我需要创建函数来检查允许用户做什么。


// for example array with all current user permissions

// this permissions mean user is allowed to do everything with tasks

// add and edit reports, but not allowed to to delete it


const permissions = ['reports.add', 'reports.edit', 'tasks'];


const isAllowed = (condition) => {

   return permissions.some((permission) => {

            // here is problem, I can't create algorithm

       });

};



// When user clicks delete report button 

// I expect to use this function like this 


if (isAllowed('reports.delete')) {

    deleteReport()

}


海绵宝宝撒
浏览 114回答 2
2回答

蛊毒传说

permissions如果condition以permission.开头,您可以搜索。const    permissions = ['reports.add', 'reports.edit', 'tasks'],    isAllowed = condition => permissions.some(permission => condition.startsWith(permission));console.log(isAllowed('reports.add')); //  trueconsole.log(isAllowed('tasks.edit'));  //  trueconsole.log(isAllowed('tasks'));       //  trueconsole.log(isAllowed('task'));        // false

慕慕森

您可以只创建一个普通函数并避免在另一个函数中使用匿名函数...function isAllowed(permission) {    return condition; //or some if-else}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答