Default Parameter Values
TmcUCAR.prototype.PlotData = function(label, x, y, color, n, vertAxis="left") { |
Message: Expected ')' Line: 382 Char: 70 Code: 0 URI: http://www.xyz.com/[path]/abc.js |
Using an equals sign (=) is part of ECMAScript 6 which was mostly supported by browsers after 2018. (This page was started 01-2017.)
w3schools.com suggests the following syntax.
function myFunction(x, y) { if (y === undefined) { y = 0; } } |
However, this should also work (observed on github).
function myFunction(x, y) { y = y || 0; } |
By Value vs By Reference
This is a real pain, all the better languages allow the programmer to control whether arguments are passed "by value" or "by reference".
Function Definitions
function myFunction(a, b) { return a * b; } // named function, no semicolon var x = function (a, b) {return a * b}; // a variable is assigned to an anonymous function // and a semicolon IS required |
When "use strict"; is specified, then the var is required for both regular variables and function variables. However, when adding methods (subroutines) to objects, it is not
"use strict"; function TmcSomeObject(){ // a function is an object, it can be expanded using ".prototype." // this could be null } TmcSomeObject.prototype.init = function( ) { // this adds a method to an existing object // the real code goes here } count = 5; // this fails - Uncaught ReferenceError: count is not defined func1 = function(){} // this fails - Uncaught ReferenceError: func1 is not defined var func1 = function(){} // this works |