S6L2DPGXUW Functions can’t be named eval or arguments

JavaScript doesn’t care how many arguments are passed in nor about the data types of those arguments.

Each function has an arguments object that holds the arguments passed in. You can access the arguments passed with arguments[0], arguments[1], etc.

Use arguments.length to find out how many arguments were passed to the function.

Function signatures are not validated since they are not required. The named arguments match up and stay in sync with the arguments object, but they don’t share the same memory space. They only stay in sync if the values are passed to the function initially:


function mytest(x, y) {
  console.log('x: ' + x + ' y: ' + y + ' arguments[0]: ' + arguments[0] + ' arguments[1]: ' + arguments[1]);

  x=5; y=10;

  console.log('x: ' + x + ' y: ' + y + ' arguments[0]: ' + arguments[0] + ' arguments[1]: ' + arguments[1]);

  x += y;

  console.log('x: ' + x + ' y: ' + y + ' arguments[0]: ' + arguments[0] + ' arguments[1]: ' + arguments[1]);

  y += x;

  console.log('x: ' + x + ' y: ' + y + ' arguments[0]: ' + arguments[0] + ' arguments[1]: ' + arguments[1]);

  arguments[0] = a; arguments[1] = b;

  console.log('x: ' + x + ' y: ' + y + ' arguments[0]: ' + arguments[0] + ' arguments[1]: ' + arguments[1]);

  arguments[0] += arguments[1];

  console.log('x: ' + x + ' y: ' + y + ' arguments[0]: ' + arguments[0] + ' arguments[1]: ' + arguments[1]);

  arguments[1] += arguments[0];

  console.log('x: ' + x + ' y: ' + y + ' arguments[0]: ' + arguments[0] + ' arguments[1]: ' + arguments[1]);
}

console.log(mytest());

// x: undefined y: undefined arguments[0]: undefined arguments[1]: undefined  -- at function start

// x: 5 y: 10 arguments[0]: undefined arguments[1]: undefined   -- x=5; y=10 - doesn't change arguments

// x: 15 y: 10 arguments[0]: undefined arguments[1]: undefined  --  x+=y  - still doesn't change arguments

// x: 15 y: 25 arguments[0]: undefined arguments[1]: undefined  --  y+=x - still no syncing

// x: 15 y: 25 arguments[0]: a arguments[1]: b  --  arguments[0] = a; arguments[1] = b; - no syncing back to the named vars

// x: 15 y: 25 arguments[0]: ab arguments[1]: b  -- adding arguments together, still no syncing

// x: 15 y: 25 arguments[0]: ab arguments[1]: bab  -- more adding, no syncing


//  NOTICE: if no variables are passed to the function, the arguments are totally separate from the named variables and are not in sync


console.log(mytest("M"));


//x: M y: undefined arguments[0]: M arguments[1]: undefined  -- one variable x = "M" passed in - both x and arguments[0] accept the value

// x: 5 y: 10 arguments[0]: 5 arguments[1]: undefined  -- changed x and y, arguments[0] changes, but not [1]

// x: 15 y: 10 arguments[0]: 15 arguments[1]: undefined  -- addition, still no change to [1] but [0] stays in sync

// x: 15 y: 25 arguments[0]: 15 arguments[1]: undefined  -- more addition - same thing, arguments[0] and x stay in sync, but not y and arguments[1]

// x: a y: 25 arguments[0]: a arguments[1]: b  -- assign arguments, x syncs, y does not

// x: ab y: 25 arguments[0]: ab arguments[1]: b  -- adding arguments, x syncs, y doesn't

// x: ab y: 25 arguments[0]: ab arguments[1]: bab  -- more adding, same thing


// NOTICE: The variables that ARE passed in stay in sync with the corresponding arguments variable


console.log(mytest("M","N"));


// x: M y: N arguments[0]: M arguments[1]: N  -- both x and y are initialized during the function call

// x: 5 y: 10 arguments[0]: 5 arguments[1]: 10  -- x and y are both set and both arguments stay in sync

// x: 15 y: 10 arguments[0]: 15 arguments[1]: 10   -- addition, both arguments sync again

// x: 15 y: 25 arguments[0]: 15 arguments[1]: 25   -- more addition, more syncing

// x: a y: b arguments[0]: a arguments[1]: b   -- set arguments, x and y both sync up

// x: ab y: b arguments[0]: ab arguments[1]: b  -- addition, x and y still syncing

// x: ab y: bab arguments[0]: ab arguments[1]: bab  -- more addition, more syncing


// NOTICE: all variables that are passed in stay in sync regardless of whether it's the named variable or the arguments variable that changes, they all stay in sync

All arguments are passed by variable. Arguments can’t be passed by reference.

Functions can not be overloaded. In other words, function names should be unique regardless of their signature. Just having different arguments does not make it a different function call. If two functions have the same name, the last function defined becomes the function that will get executed using that name.

Overloading can be simulated by counting the number of objects and using logic to handle the variables differently.

Leave a Comment

Your email address will not be published. Required fields are marked *