javascript函数名冲突
我有两个 JavaScript 文件,其中包含一个同名 $() 的 jQuery 函数。
如何区分两个同名的函数?
I have two JavaScript files that contain a jQuery function with the same name $().
How can I distinguish between the two identically named functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简短的回答:你不能。所有包含的 js 文件都位于同一名称空间中。
Short answer: You can't. All includes js files live in the same namespace.
重命名一个。
Rename one.
尝试为您从事的每个项目使用命名空间。例如,不要编写一个名为 dosomething() 的函数,而是将其命名为 myproject.dosomething()
这样,它是特定于项目的,并且您将避免拥有两个相同名称的函数,即使您包含的文件不是您自己开发的。
Try using a namespace for each project you work on. For instance, instead of writing a function called dosomething(), call it myproject.dosomething()
That way, it is project specific and you will avoid having two identically named functions, even if you are including files you did not develop yourself.
也许您想要 将 jQuery 与其他库结合使用,但我无法从问题。也许您可以包含一些您遇到的问题的示例代码?
Perhaps you want Using jQuery with Other Libraries, but I can't tell exactly what your problem is from the question. Perhaps you could include some sample code of the problem you're having?