Definition of Handling |
Learn the basic rules for opening JavaScript and making definitions.
Contents of js File -- Various Rules
Next we will learn the [Definition of handling] using our prepared file.
First download sample.zip file. (This should contain a JavaScript file named “myscript.js” and an HTML file named “sample.html”.)
When you open myscript.js using Hidemaru Editor, you should see that the top portion would look as follows.
//--------------------------------------------------- // A message is displayed in a new window. //------------------------ function oshita() { window.alert('Thank you very much. Have a good day!'); } //---------------------------------------------------
In JavaScript, two slashes “//” means that everything thereafter to the end of the line is a comment. Therefore, the above top three lines and the last line are the comment lines. These explain the contents of the defined handling, and the scope from where to where is the definition.
The handling in JavaScript is usually defined as following.
function handling name (input 1, input 2, ...) {
handling 1;
handling 2;
.......
.......
}
To define handling, write “function” first, followed by the arbitrary handling name you defined. In the previous example, “oshita” is the handling name.
Next describe the input (generally called argument) necessary for the handling, enclosing it in parentheses “( )”.When there are multiple inputs (arguments), separate them by commas, “,”.Even when no input (arguments) are necessary, you cannot omit parentheses “()” . The handling “oshita” shown earlier is a handling that does not need an argument.
Lastly, describe the contents of handling to be defined, with the entirety being enclosed by “{ }”. At the end of each handling, put a semicolon “;”. In the example of “oshita”, a single handling of window.alert() is to be executed once.