JSLint“超出范围”功能排序导致的错误? [英] JSLint "out of scope" error due to function ordering?

查看:84
本文介绍了JSLint“超出范围”功能排序导致的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSLint似乎对功能排序很挑剔。

JSLint seems to be picky about function ordering.

这好转:

function a() {
    'use strict';
    return 1;
}

function b() {
    'use strict';
    a();
}

虽然这给出了'a'不在范围错误消息:

While this gives an 'a' is out of scope error message:

function b() {
    'use strict';
    a();
}

function a() {
    'use strict';
    return 1;
}

这是设计的吗?我应该关心吗?如何避免在较大(较复杂)的情况下避免给函数一个明确的顺序?

Is this by design? Should I care? How can it be avoided in larger (more complex) cases, where it might not always be possible to give the functions a clear order?

推荐答案

JSLint / JSHint希望您在引用它们之前定义函数。但是,JavaScript并不关心,因为函数和变量已悬挂

JSLint/JSHint expect you to define functions before you reference them. However, JavaScript doesn't care because functions and variables are hoisted.

您可以使用 http://jshint.com/docs/options/#latedef

/* jshint latedef:nofunc */
function b() {
    'use strict';
    a();
}

function a() {
    'use strict';
    return 1;
}

参见 https://stackoverflow.com/a/23916719/227299

这篇关于JSLint“超出范围”功能排序导致的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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