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 - Moment.js - "Accessing Moment through the global scope" warning message - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Moment.js - "Accessing Moment through the global scope" warning message - Stack Overflow

programmeradmin2浏览0评论

I've recently downloaded last version of moment.js and it begins to show the following message when trying to call, for example, moment().add(1, 'day');

"Deprecation warning: Accessing Moment through the global scope is deprecated, and will be removed in an uping release."

Which is the best way to call moment methonds?

Update: Figured out the problem

The problem was present because I have requirejs in my project and momentjs was trying to warn me that I should use momentjs as a module dependency instead.

The following code was extracted from momentjs v2.9.0

// CommonJS module is defined
if (hasModule) {
    module.exports = moment;
} else if (typeof define === 'function' && define.amd) {
    define('moment', function (require, exports, module) {
        if (module.config && module.config() && module.config().noGlobal === true) {
            // release the global variable
            globalScope.moment = oldGlobalMoment;
        }

        return moment;
    });
    makeGlobal(true);
} else {
    makeGlobal();
}

//And this is the 'makeGlobal' function. globalScope
function makeGlobal(shouldDeprecate) {
    /*global ender:false */
    if (typeof ender !== 'undefined') {
        return;
    }
    oldGlobalMoment = globalScope.moment;
    if (shouldDeprecate) {
        globalScope.moment = deprecate(
                'Accessing Moment through the global scope is ' +
                'deprecated, and will be removed in an uping ' +
                'release.',
                moment);
    } else {
        globalScope.moment = moment;
    }
}

So, if I use this library in a CommonJS environment, then I should use import statement.

If I use requirejs, then I should include momentjs as a dependency of my modules.

Finally, if neither the other cases acplish, then I can use it directly from global scope (window object in browser)

I've recently downloaded last version of moment.js and it begins to show the following message when trying to call, for example, moment().add(1, 'day');

"Deprecation warning: Accessing Moment through the global scope is deprecated, and will be removed in an uping release."

Which is the best way to call moment methonds?

Update: Figured out the problem

The problem was present because I have requirejs in my project and momentjs was trying to warn me that I should use momentjs as a module dependency instead.

The following code was extracted from momentjs v2.9.0

// CommonJS module is defined
if (hasModule) {
    module.exports = moment;
} else if (typeof define === 'function' && define.amd) {
    define('moment', function (require, exports, module) {
        if (module.config && module.config() && module.config().noGlobal === true) {
            // release the global variable
            globalScope.moment = oldGlobalMoment;
        }

        return moment;
    });
    makeGlobal(true);
} else {
    makeGlobal();
}

//And this is the 'makeGlobal' function. globalScope
function makeGlobal(shouldDeprecate) {
    /*global ender:false */
    if (typeof ender !== 'undefined') {
        return;
    }
    oldGlobalMoment = globalScope.moment;
    if (shouldDeprecate) {
        globalScope.moment = deprecate(
                'Accessing Moment through the global scope is ' +
                'deprecated, and will be removed in an uping ' +
                'release.',
                moment);
    } else {
        globalScope.moment = moment;
    }
}

So, if I use this library in a CommonJS environment, then I should use import statement.

If I use requirejs, then I should include momentjs as a dependency of my modules.

Finally, if neither the other cases acplish, then I can use it directly from global scope (window object in browser)

Share Improve this question edited Apr 14, 2015 at 12:44 edrian asked Dec 15, 2014 at 12:51 edrianedrian 4,5315 gold badges33 silver badges35 bronze badges 7
  • And this is in a clientside script ? – adeneo Commented Dec 15, 2014 at 12:54
  • 1 If you're not using requireJs the last I saw was that this was an issue, re: github./moment/moment/issues/1214. It may have been resolved on master, but I don't know. – Dave Newton Commented Dec 15, 2014 at 12:58
  • It seems like you're using moment for nodeJS... you could double check and get a version of moment build for the browser. – Adrian Salazar Commented Dec 15, 2014 at 12:58
  • Try getting moment from cdnjs momentjs./docs/#/use-it/browser – Adrian Salazar Commented Dec 15, 2014 at 12:59
  • @AdrianSalazar, I've tried using momentjs from the cdn link you posted but the problem still exists – edrian Commented Dec 15, 2014 at 13:06
 |  Show 2 more ments

2 Answers 2

Reset to default 10

You can use requirejs to pull it in rather than using the global scope:

require.config({
    paths: {
        "moment": "path/to/moment",
    }
});

define(["moment"], function (moment) {
    moment().format();
});

Taken from http://momentjs./docs/

This really isn't an answer, but an appreciation of the problem:

  1. There has yet to be a cogent explanation of why the deprecation occurs, or a newbie explanation of what it is. Specifically, not paragraph saying 'if you do it the old way, it breaks in a subtle way'. The closest is a bug report that, using node, a single symbol is defined in the global namespace (https://github./moment/moment/issues/1214), which is mostly philosophy.

  2. The deprecation es on usage, so its unclear to people why. It appears to need be fixed in installation.

  3. No one on any chat node has explained it, outside of mirroring the require.js boilerplate. The ment seems to continue as "do it this way and it works". The boilerplate does not cover all users.

  4. Some failing lines include simple constructors like moment(value), which is the whole point of the library.

  5. It appears the minor version upgrade from moment 2.9.0 to 2.10.0 may have forced deprecated code to break, at least for those using ECMAScript and a fallback. Reverting to 2.9.0 will allow you to keep working for now. If you only had the related breakage of moment.duration.fn disappearing, you can upgrade to 2.10.1 or above.

发布评论

评论列表(0)

  1. 暂无评论