te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Tool that detects duplicate javascript function names in a web page? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Tool that detects duplicate javascript function names in a web page? - Stack Overflow

programmeradmin4浏览0评论

Background

We have a web application in which several developers have written several .js files that manipulate the DOM and the problem of duplicate function names has crept into our application.

Question

Can anyone remend a tool that will warn us when we accidentally write a web page with two javascript functions with the same name?

Example

HTML page

<script language="JavaScript" src="test.js" type="text/javascript"></script>
<script>function foo() {alert('bar');}</script>

test.js

function foo() {alert('foo');}

Since foo() is declared twice in the page, apparently only the one that takes precedence is loaded.

The tools I've used seem to ignore this. Firebug only shows the loaded function. Netbeans will show both functions in navigator (without a warning), but only looks at one file at a time (ie, I can't point it at the HTML file and see it and the .js at the same time.) and web developer extensions allows me to look at everything at once, but no errors or warnings. Firefox's error console also throws no warning or error. Neither does IE.

Thanks!

Background

We have a web application in which several developers have written several .js files that manipulate the DOM and the problem of duplicate function names has crept into our application.

Question

Can anyone remend a tool that will warn us when we accidentally write a web page with two javascript functions with the same name?

Example

HTML page

<script language="JavaScript" src="test.js" type="text/javascript"></script>
<script>function foo() {alert('bar');}</script>

test.js

function foo() {alert('foo');}

Since foo() is declared twice in the page, apparently only the one that takes precedence is loaded.

The tools I've used seem to ignore this. Firebug only shows the loaded function. Netbeans will show both functions in navigator (without a warning), but only looks at one file at a time (ie, I can't point it at the HTML file and see it and the .js at the same time.) and web developer extensions allows me to look at everything at once, but no errors or warnings. Firefox's error console also throws no warning or error. Neither does IE.

Thanks!

Share Improve this question asked Nov 17, 2008 at 15:16 stevesteve 9641 gold badge10 silver badges18 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

Well, using a parser may not always be ideal as it requires an extra step of copying and pasting your code, and everyone else's, into the parser and even then I'm not sure it would catch what you want. The time tested solution to collaborative Javascript development is to namespace your code.

var myNamespace = function (){
   var myPrivateProperty;
   var myPrivateFunction = function(){};
   return{

      myPublicProperty: '',
      myPublicFunction: function(){}


   }

 }();

This is based on Douglas Crockford's module pattern.

Then you can call your public functions this way:

 myNamespace.myPublicFunction();

And your public properties:

 myNamespace.myPublicProperty;

Each developer can develop in their own namespace so as to not step on others' code.

I have often used JSLINT

In short it is a "piler" for JavaScript using JavaScript. I have learned a lot by watching Douglas Crockford’s training videos.

It not only checks for duplicate functions, but global variables, and a whole bunch of other things. Like Douglas said in one of his videos it allows you to only use the good bits of JavaScript

ScriptManager.RegisterClientScriptInclude(Me, Page.GetType, "jsTest", "test.js")

My solution would be a simple HTML parser for Java (I just hacked one with regexps; you may want to try http://java-source/open-source/html-parsers/jtidy) and the Apache Rhino library (http://www.mozilla/rhino/doc.html).

It should be possible to overload the parser in Rhino to throw an error if a function is redefined.

I'm using a slightly modified version of env.js (http://jqueryjs.googlecode./svn/trunk/jquery/build/runtest/env.js) by John Resig to emulate a browser. This allows me to test JavaScript in JUnit tests. I can fill in forms, submit them, check the layout of the pages, etc.

See my blog for details: http://blog.pdark.de/2008/11/18/testing-the-impossible-javascript-in-a-web-page/

发布评论

评论列表(0)

  1. 暂无评论