简体   繁体   中英

Why this function return nothing?

I use Angular Providers. While i call master.parameters() I got nothing. 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

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

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

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 
{
    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

This is why it's a good idea to jshint/jslint your code - or at least look at the console for errors like this

newline after a return is only problem. Thanks @aromandaX

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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