Explanation requested for fixedEncodeURIComponent

狂风中的少年 提交于 2019-12-11 18:38:16

问题


I'm wondering if anyone can explain this function to me? I've tested it and it works like a dream but I don't understand how!

It's from the MDN reference here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

function fixedEncodeURIComponent (str) {
  return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
}

I understand replace as the match followed by the replacement, what I am struggling with is the escape reference and the secondary replacement which results in actual encode values replacing characters e.g. ( = %28 and )= %29.


回答1:


The reference to "escape" is just a reference to the global function of that name. If the second argument to .replace() is a function, then JavaScript passes the matched string to the function and replaces it with whatever the function returns.

Try typing

escape("!")

in your browser's console.



来源:https://stackoverflow.com/questions/24663857/explanation-requested-for-fixedencodeuricomponent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!