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; } ?>javascript - Calling a function with same name in another JS file - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calling a function with same name in another JS file - Stack Overflow

programmeradmin4浏览0评论

I'm just a bit confused here... If I have one .js file with function like this:

function myMain() {
    var count=0;
    count++;               
    myHelper(count);
    alert(count);
}

function myHelper(count) {
    alert(count);
    count++;
}

Can I still call another method myHelper() on the other .js file? Or is there any other way that I can pass the count variable from one function to another then it will be called to other .js file. Do you have any idea regarding this one? Thanks!

I'm just a bit confused here... If I have one .js file with function like this:

function myMain() {
    var count=0;
    count++;               
    myHelper(count);
    alert(count);
}

function myHelper(count) {
    alert(count);
    count++;
}

Can I still call another method myHelper() on the other .js file? Or is there any other way that I can pass the count variable from one function to another then it will be called to other .js file. Do you have any idea regarding this one? Thanks!

Share Improve this question edited Feb 8, 2013 at 4:28 Samuel Liew 79.1k111 gold badges168 silver badges300 bronze badges asked Feb 8, 2013 at 4:15 ninpot18ninpot18 1271 gold badge6 silver badges16 bronze badges 1
  • 1 good practice to use namespacing, especially if you have two global methods with the same name – Samuel Liew Commented Feb 8, 2013 at 4:19
Add a ment  | 

3 Answers 3

Reset to default 16

Update: Nowadays you should prefer to use ES6 import/export in a <script> tag with type="module" or via a module bundler like webpack.


When both script files are included in the same page, they run in the same global JavaScript context, so the two names will overwrite each other. So no, you can not have two functions in different .js files with the same name and access both of them as you've written it.

The simplest solution would be to just rename one of the functions.

A better solution would be for you to write your JavaScript modularly with namespaces, so that each script file adds the minimum possible (preferably 1) objects to the global scope to avoid naming conflicts between separate scripts.

There are a number of ways to do this in JavaScript. The simplest way is to just define a single object in each file:

// In your first script file
var ModuleName = {
    myMain: function () {
        var count=0;
        count++;               
        myHelper(count);
        alert(count);
    },

    myHelper: function (count) {
        alert(count);
        count++;
    }
}

In a later script file, call the function ModuleName.myMain();

A more popular method is to use a self-evaluating function, similar to the following:

(function (window, undefined) {

    // Your code, defining various functions, etc.
    function myMain() { ... }
    function myHelper(count) { ... }

    // More code...

    // List functions you want other scripts to access
    window.ModuleName = {
        myHelper: myHelper,
        myMain: myMain  
    };
})(window)

If you know you are about to overwrite a method, you can store the old one first in a variable and then call the other implementation of the function via that variable.

Yes, you can call myHelper() from any other js file as long as you include both js files in one html or jsp page

you may want to look at this : Can we call the function written in one JavaScript in another JS file?

发布评论

评论列表(0)

  1. 暂无评论