首页 > 其他分享 >js中的钩子(hook)机制

js中的钩子(hook)机制

时间:2022-08-19 14:33:51浏览次数:46  
标签:code name item 钩子 js hook score praise

注:代替if-else,switch-case,提高程序运行效率

hook机制也就是钩子机制,由表驱动实现,常用来处理多种特殊情况的处理

  let list = {
        '1':'test1',
        '2':'test2',
        '3':'test3',
        '4':'test4',
    }
    var code = 4
    if(code){
        alert(list[code])
    }
var arr = [{name: 'liming', score: 70, praise: 1},
 {name: 'liyi', score: 90, praise: 2},
 {name: 'liuwei', score: 80, praise: 3},
 {name: 'ertuzi', score: 85, praise: 3}]
 var rule = {
        1:20,
        2:10,
        3:5
    }
arr.forEach((item,index)=>{
   if(item.praise)
   item.score += rule[item.praise]
})
console.log(arr) //此处修改原数组

 

标签:code,name,item,钩子,js,hook,score,praise
From: https://www.cnblogs.com/ljingjing/p/16601872.html

相关文章