How can I call Android JavaScript functions in WebView?

In JavaScript, since global variables are available to functions, is there ever any reason to pass them to functions through arguments?

  • In JavaScript, since global variables are available to functions, is there ever any reason to pass them to functions through arguments?

  • Answer:

    Namespace pollution. Also, you often will want to pass different variables/values to the same function. Also, good in program design a function takes a number of inputs and does something with them, possibly returning something. Generally, it's a good idea to stick to this.

judytaos at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

Just because a language gives you features that allow you to write bad code, it doesn't mean you're required to use them.

Obscure Reference

Douglas Crockford on JavaScript: http://javascript.crockford.com/code.htmlAll variables should be declared before used. JavaScript does not require this, but doing so makes the program easier to read and makes it easier to detect undeclared variables that may become implied globals. Implied http://yuiblog.com/blog/2006/06/01/global-domination/ variables should never be used. ... JavaScript does not have block scope, so defining variables in blocks can confuse programmers who are experienced with other C family languages. Define all variables at the top of the function. Use of global variables should be minimized. Implied global variables should never be used.And in http://yuiblog.com/blog/2006/06/01/global-domination/, again by Crockford:The global object is the global namespace that holds the top level functions and global variables. Variables which are not explicitly defined are implied global variables, and their names are also kept in the global object. This was a convenience for the little scripts that Navigator 2 was expected to support. In the decade since NS2, those little scripts have grown into Ajax applications, and the true nature of the global object is revealed: Global variables are evil. Global variables are a source of unreliability and insecurity. Fortunately, JavaScript includes tools for allowing us to drastically minimize our use of globals, which makes our programs more robust. This becomes increasingly important as our programs get bigger, and as we mix in and mash up program components from multiple authors. Reducing our dependency on globals increases the likelihood that collisions are avoided and that the program components work harmoniously.

artlung

Using functions also encourages code organization. One of the main questions you should be asking yourself when you design software is “How much code do I have to read to understand what this does?” When you find a bug in the global variable, you might have to search your entire codebase and read everything that could have modified it.

yaymukund

good in in good

iotic

Makes it much easier to see at a glance what the function modifies. When you are hunting down a bug late at night you will be glad of it!

emilyw

Testing. You should be (able to be) writing code suites that exercise your functions and make sure they do what you think they're doing. The only way to write the fifty ways your code can break and try them all is to make the function take parameters that you construct before you call it, and test the result once it returns.

cmiller

Not only should you be using functions, but it is will worth leaning how to do object oriented programming in Javascript. It isn't all that hard to build fairly sophisticated Javascript apps if you architect your code properly.

b1tr0t

Lots! Here's a concrete example: Suppose you have a loop that used the variable i, that is global. And then you call another function inside the loop which also uses your variable i. Then that's going to interfere with the value of i, and break everything in mysterious and irritating to debug ways. Generally speaking, using global variables makes your code harder to understand and debug in exactly the way. If you really need globals for some reason, it's a good idea to put them all inside a big global variable like MYAPPLICATION so that it's obvious that they're global when you're referring to them. (as artlung mentioned)

oranger

Well, one specific case where you might pass a global variable to a function is where that function will return another function that captures the current value of that variable; here's a contrived example: function makeHelloFn (name) {   return( function () { console.log( "Hello " + name + "!" ) } ); } foo = "Nic"; helloNic = makeHelloFn(foo); foo = "Judy"; helloJudy = makeHelloFn(foo); helloNic(); // logs "Hello Nic!" helloJudy(); // logs "Hello Judy!" The returned functions are called "closures" and this is a very common JavaScript programming technique. Note that global variables are indeed evil and this is not code I would ever really write ツ

nicwolff

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.