简体   繁体   English

使用js文件名调用JavaScript函数

[英]Call javascript function using js filename

I have two js files . 我有两个js文件。 One makes use of prototype syntax and the jQuery . 一种利用prototype语法和jQuery Somehow they don't work together. 他们以某种方式无法一起工作。 I can try to call the functions inside the files using the filename.functionname something like that. 我可以尝试使用filename.functionname之类的文件来调用文件中的函数。

Please tell me the syntax to do so OR is there any other way to make protoype and jQuery run together since some part of their syntax clashes. 请告诉我这样做的语法,或者还有其他方法可以使protoypejQuery一起运行,因为它们的某些语法冲突。

You can't call function by the file name. 您不能通过文件名调用函数。

Use jquery noCoflicts 使用jQuery noCoflicts

Short example from the docs: 来自文档的简短示例:

<script type="text/javascript" src="other_lib.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
  $.noConflict();
  jQuery(document).ready(function($) {
    // Code that uses jQuery's $ can follow here.
  });
  // Code that uses other library's $ can follow here.
</script>

It is recommended not to use more than one javascript library at the same page, there are a lot of reasons for that, but as I see it the most important is: why load 2 different frameworks? 建议不要在同一页面上使用多个JavaScript库,这有很多原因,但是据我所知,最重要的是:为什么要加载2个不同的框架? they usually are quite big in size and in most cases have a lot of shared functionality. 它们通常很大,并且在大多数情况下具有很多共享功能。

You can however try to do so with this: http://docs.jquery.com/Using_jQuery_with_Other_Libraries 但是,您可以尝试这样做: http : //docs.jquery.com/Using_jQuery_with_Other_Libraries

As for the syntex to call a method using the file name, that does not exist. 至于使用文件名来调用方法的syntex,则不存在。 Once the browser loads javascript code it adds it to the global scope of the document. 浏览器加载javascript代码后,会将其添加到文档的全局范围。 What you can do is put things inside "namespaces", something like: 您可以做的是将内容放入“命名空间”中,例如:

var MyNamespace = {
  methodA: function() {
    // do something
  },
  methodB: function() {
    // do something
  }
};

MyNamespace.method();

The problem though is that you are using 3rd party libraries which are not wrapped in namespaces, at least not all of their code. 但是问题是,您正在使用的第三方库没有包装在名称空间中,至少不是全部代码都包装在其中。

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

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