简体   繁体   English

为什么这个函数什么都不返回?

[英]Why this function return nothing?

I use Angular Providers.我使用 Angular 提供者。 While i call master.parameters() I got nothing.当我调用 master.parameters() 时,我一无所获。 Why ?为什么 ?

       masterApp.provider('master',[function() { 
          this.$get = function()
          {
                return {

                    parameters: function() {
                        var data="hai";
                        return
                           data;

                    }
                };

          };

    }]);

Javascript was a language written in a handful of days, therefore it has many many many quirks that have never been, and probably wont ever be, eradicated for fear of breaking the internets Javascript 是一种在几天内编写的语言,因此它有很多很多怪癖从未,也可能永远不会被根除,因为害怕破坏互联网

one of these is javascripts loose, very very loose, requirement for statement termination by the good ol' semi-colon - the Javascript Engine "infers" where this should be其中之一是 javascripts 松散,非常松散,要求用好的 ol' 分号终止语句 - Javascript 引擎“推断”这应该在哪里

one place all Javascript Engines seem to agree to get wrong consistently, despite the "unreachable code after return statement" error that is invariably output in the console by reputable browsers, is the humble return statement尽管信誉良好的浏览器总是在控制台中输出“返回语句后无法访问代码”错误,但所有 Javascript 引擎似乎都同意始终出错的一个地方是不起眼的返回语句

return
   'value'

is always interpreted as总是被解释为

return;
value;

the most common gotcha is returning an object, some people like to have the open and close braces on their own line so they write the return statement as最常见的问题是返回一个对象,有些人喜欢在自己的行中使用左大括号和右大括号,所以他们将 return 语句写为

return 
{
    key: value,
    key2: value
}

which, as we've seen will be interpreted as正如我们所见,这将被解释为

return;
{
    key: value,
    key2: value
};

with a console message about unreachable code after a return statement在 return 语句后显示有关无法访问代码的控制台消息

This is why it's a good idea to jshint/jslint your code - or at least look at the console for errors like this这就是为什么 jshint/jslint 你的代码是个好主意 - 或者至少查看控制台是否有这样的错误

newline after a return is only problem.返回后换行只是问题。 Thanks @aromandaX谢谢@aromandaX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM