実際にはかなり単純で、関数の外で定義するだけです。
編集:例と、何が行われたか、どのように機能するかを説明するコメントで更新されました。
DegreesToMils.degrees = 10; /* This is a static variable declaration to make sure it isn't undefined
* See note 1 below */
function DegreesToMils(degrees) {
if (degrees !== undefined) {
DegreesToMils.degrees = degrees; /* If the parameter is defined,
* it will update the static variable */
}
var milsPerDegree = 17.777778; /* This is a variable created and accessible within the function */
return DegreesToMils.degrees * milsPerDegree; /* The function will return 177.77778 */
}
console.log(DegreesToMils.degrees); /* Prints 10, Note 1: This would be undefined if
* not declared before the first call to DegreesToMils() with a
* defined parameter
*/
console.log(DegreesToMils(10)); /* Prints 177.77778 */
console.log(DegreesToMils(9)); /* Prints 160.00000200000002, Sets DegreesToMils.degrees to 9 */
console.log(DegreesToMils.degrees); /* Prints 9 */