在javascript中,相同的代码解析不同函数中的参数 - 复制或不复制?
有一系列函数,每个函数都以相同的方式解析其参数。现在这个解析块被剪切并粘贴到每个函数的开头。有更好的方法吗?
Post.update = function( e ) { // parse args var e, el, orig_el, args, render, callBack; for( var i=0; i<arguments.length; i++ ) { if( arguments[i] instanceof HTMLElement ) orig_el = arguments[i]; else if( arguments[i] instanceof Render ) render = arguments[i]; else if( arguments[i] instanceof Event ) { e = arguments[i]; orig_el = orig_el || e.target; } else if( typeof arguments[i] == 'function' ) callBack = arguments[i]; } // Post.update work here } Post.verify = function( e ) { // parse args ... // Post.verify work here }
解析参数而不是单独传递的原因是,有五个以上可能的参数
我很容易在排序和遗漏调用具有长参数列表的函数时犯错误
更改函数的一个参数意味着更改对 func 的每次调用
imho 函数与准确传递必要的内容相比,
所以我的目标是抽象函数的解析部分,同时保持函数的清晰度像现在一样打电话。
Have a series of functions, each of which parses its arguments identically. right now this parsing chunk is cut and pasted to the beginning of each function. Is there a better way to do this?
Post.update = function( e ) { // parse args var e, el, orig_el, args, render, callBack; for( var i=0; i<arguments.length; i++ ) { if( arguments[i] instanceof HTMLElement ) orig_el = arguments[i]; else if( arguments[i] instanceof Render ) render = arguments[i]; else if( arguments[i] instanceof Event ) { e = arguments[i]; orig_el = orig_el || e.target; } else if( typeof arguments[i] == 'function' ) callBack = arguments[i]; } // Post.update work here } Post.verify = function( e ) { // parse args ... // Post.verify work here }
The reasons arguments are parsed instead of passed individually are that, with five+ possible args
I'm prone to make mistakes in ordering and omission calling funcs with a long list of args
changing one argument to the function means changing every call to the func
imho a function with five arguments is quite unreadable compared to passing exactly what's necessary
So my goal is to abstract the parse section of the functions while maintaining the clarity of the function calls as they are now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当然,您应该使用一个函数来防止复制和粘贴所有代码!
如果您想更改代码中的单个字符怎么办?现在您必须再次复制并粘贴所有内容!这真的没什么好处。
将代码粘贴到函数中并每次调用它,这样当您更改代码函数中的某些内容时,更改将反映到使用该函数的每个地方。
您是否担心向函数传递这么多参数?你真的必须担心,因为将这么多参数传递给一个函数真的很糟糕。只需将所有参数放入一个对象中,然后仅将该对象传递给函数,如下所示:
然后在函数内,您将有一个参数,并且可以像这样访问所有其他参数:
Of course you should use a function to prevent copy and paste all that code!
What if you want to change a single character in the code? Right now you have to copy and paste everything again! That's just no good really.
Stick that code into a function and call it everytime, this way when you change something in the code function, the change will be reflected to every place where that function is being used.
Are you worried about passing so many arguments into the function? You really have to be worried, cause passing so many arguments into a function is really bad. Just stick all your arguments into a single object, and only pass that object to the function, like this:
Then inside the function you will have a single argument and you can access all the others like this:
您应该意识到函数不应有超过 3 个参数,而不是从函数中解析 5 个参数。
你想要做的是让最后一个参数是一个对象
然后只需要一个可以重用的函数
Rather then parsing 5 arguments out of a function you should realise functions should not have more then 3 arguments.
What you want to do instead is have the last argument be an object
Then just have a function for that can be re-used
定义一次函数并重用它们
Define the functions once and reuse them
为什么不使用 Post.parseArgs 函数?
Why not using a Post.parseArgs function ?