函数声明不应放在块中。使用函数表达式或将语句移动到外部函数的顶部 [英] Function declarations should not be placed in blocks. Use a function expression or move the statement to the top of the outer function

查看:342
本文介绍了函数声明不应放在块中。使用函数表达式或将语句移动到外部函数的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

if (typeof console === "object" && typeof console.error === "function") {
    function e(msg) {"use strict"; console.info(msg);}
}

jsLint给出了以下错误:

For which jsLint gives the following error:


不应将函数语句放在块中。使用函数表达式或将语句移动到外部函数的顶部。

Function statements should not be placed in blocks. Use a function expression or move the statement to the top of the outer function.

为什么它会出现此错误,它是什么意思?

Why is it giving this error and what does it mean?

推荐答案

你不应该在if中创建函数。你做得好多了:

You should not be creating function inside if block. You are much better off doing:

var e = function(){};

if(typeof console === "object" && typeof console.error === "function"){
    e = function (msg){ ... };
}

这篇关于函数声明不应放在块中。使用函数表达式或将语句移动到外部函数的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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