简体   繁体   English

javascript函数名称冲突

[英]javascript function name collision

I have two JavaScript files that contain a jQuery function with the same name $(). 我有两个JavaScript文件,其中包含一个名为$()的jQuery函数。

How can I distinguish between the two identically named functions? 如何区分两个同名的函数?

Short answer: You can't. 简答:你做不到。 All includes js files live in the same namespace. 所有包含js文件存在于同一名称空间中。

重命名一个。

$ = typeof $ !== 'function' ? $ : $foo;

Perhaps you want Using jQuery with Other Libraries , but I can't tell exactly what your problem is from the question. 也许你想要将jQuery与其他库一起使用 ,但我无法确切地说出你的问题是什么。 Perhaps you could include some sample code of the problem you're having? 也许您可以包含一些您遇到的问题的示例代码?

Try using a namespace for each project you work on. 尝试为您使用的每个项目使用命名空间。 For instance, instead of writing a function called dosomething(), call it myproject.dosomething() 例如,不要编写一个名为dosomething()的函数,而是将其命名为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. 这样,它是特定于项目的,即使您包含自己没有开发的文件,也可以避免使用两个具有相同名称的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM