Javascript代码组织 [英] Javascript code organization

查看:98
本文介绍了Javascript代码组织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近才开始使用jQuery&开发了一个小应用程序.但是由于大量的JavaScript代码,页面大小正在增加,因此我开始将代码移到单独的脚本文件中.但是,

1)当我将代码移到单独的js文件时,因为它们位于其他脚本文件中,所以我无法访问其他jquery插件函数.如何在单独的脚本文件中使用其他库?

2)我做错了吗?这在我单独的脚本文件(foo.js)中根本不起作用.它说stringify不是函数

I just started using jquery lately & developed a small application. However the pagesize was increasing because of a lot of javascript code so I am starting to move the code in separate script files. However,

1) I cannot access other jquery plugins functions when I move my code to separate js file because they are located in other script files. How do I use the other libraries in my separate script file ?

2) Am I doing it the wrong way ? This does not work at all in my separate script file (foo.js). It says stringify is not a function

<script src="../assets/scripts/jquery.json-2.2.min.js" type="text/javascript">
</script>

$(document).ready(function(){
    JSON.stringify(''test message'');
});

推荐答案

(document).ready(function(){ JSON.stringify(''测试消息''); });
(document).ready(function(){ JSON.stringify(''test message''); });


听起来像是您的外部脚本未正确引用.如果您的示例位于应用程序的根目录中,那么我猜您的路径将不会是:

sounds to me like your external script isn''t being referenced correctly. if your example is in the root of your application I''d guess your path isn''t going to be:

<script src="../assets/scripts/jquery.json-2.2.min.js" type="text/javascript">
</script>



但更可能是这样:



but more likely this:

<script src="assets/scripts/jquery.json-2.2.min.js" type="text/javascript">
</script>



但是如果没有更多信息,我将无法确定100%.



but I can''t be 100% sure without further information.



首先检查与您的浏览目录有关的文件路径,使用firebug查看是否实际加载了参考文件,最后在js文件顶部使用此代码添加参考

First of all check the file path respect to your browsing directory, use firebug to see reference file actually being loaded or not and finally use this code at top of the js file to add reference
/// <reference path="../assets/scripts/jquery.json-2.2.min.js" />


请按照下面的讨论
http://www.eggheadcafe.com/community/aspnet/3/10131184/how-to-add-javacript-file-reference-in-another-javascript-file.aspx [


follow the discussion below
http://www.eggheadcafe.com/community/aspnet/3/10131184/how-to-add-javacript-file-reference-in-another-javascript-file.aspx[^]

hope this will help.


这篇关于Javascript代码组织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆