-1

I want to access my routine parameters like in a bash script, by using the dollar prefix and the parameter number ($1 = first parameter, $2 = second parameter), my function signature must be empty.

function foo (/* Empty */) {
    return $1 + $2 + $3;
}

foo(2, 2, 4); // => 8

How can i do this? I tried using the apply method without success.

foo.apply(null, { $1: 2, $2: 2, $3: 4 });
tripleee
  • 175,061
  • 34
  • 275
  • 318
user123123123
  • 318
  • 2
  • 7

3 Answers3

2

Please do not use this in production code:

Object.defineProperties(window, {
  '$1': { get: function fn() { return fn.caller.arguments[0]; } },
  '$2': { get: function fn() { return fn.caller.arguments[1]; } },
  '$3': { get: function fn() { return fn.caller.arguments[2]; } },
  '$4': { get: function fn() { return fn.caller.arguments[3]; } },
  '$5': { get: function fn() { return fn.caller.arguments[4]; } }
});

var a = function() {
  console.log($1, $2, $3, $4, $5);
};

a("Hello", "I can't beleave", "this", "actually", "works!");

Wont work with "use strict" though.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
0

All javascript functions have a hidden arguments parameter.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

function foo() {
  for(var i = 0; i < arguments.length; i++) {
    this['$'+(i+1)] = arguments[i]
  }
  console.log($1, $2);
}
foo(1, 'b');
Gabriel Littman
  • 552
  • 3
  • 13
  • I want to a access the arguments like in a bash script. Not using the arguments object. – user123123123 Jun 08 '16 at 20:53
  • 1
    @user123123123 You can't. – Ismael Miguel Jun 08 '16 at 20:58
  • You are not being clear. What should $1 be? The first parameter passed to the function? Or do you want it to be a parameter from the command line when running the script. Assuming you are using node.js to run it. – Gabriel Littman Jun 08 '16 at 21:02
  • can't do it. use arguments[0] for $1 or you can write a mapping function to make it args['$1] but I don't think variables can start with a $ in javascript. – Gabriel Littman Jun 08 '16 at 21:07
  • 1
    "I don't think variables can start with a $ in javascript" - jQuery uses $ variable by default (`var $ = function(...) {...}`) - I'm sure that variables can start with $ – pzmarzly Jun 08 '16 at 21:11
  • good point... I've updated my example. It works but there might be side effects when working with nested functions in this way. – Gabriel Littman Jun 08 '16 at 21:23
  • Think about it this is really just a generalized solution from @PawełZmarzły since this === window in browsers. – Gabriel Littman Jun 08 '16 at 21:25
  • @up, I've just changed display name from Paweł Zmarzły to pzmarzly to simplify mentioning me in comments – pzmarzly Jun 08 '16 at 21:32
  • By convention, the dollar sign ($) is permitted to be used anywhere in a JavaScript identifier (Source: Ecma Script documentation (7.6 Identifiers, ECMA-262, 3rd Ed.) – user123123123 Jun 08 '16 at 21:35
0

Well, converting parameters into bash-like variables is not that simple and requires using objects (more) - either using global window object - then parameters are visible outside the function, or using local object to store parameters - then it's not 100% bash-like.

Code - using window:

function a() {
  for (var i = 0; i < arguments.length; i++) {
    window['$' + (i + 1)] = arguments[i];
  }
  console.log($1); // 12
}

a(12, 34, "a");
console.log($1); // 12 - visible outside function
console.log($2); // 34
console.log($3); // "a"

Code - using object:

function a() {
  var vars = {};
  for (var i = 0; i < arguments.length; i++) {
    vars['$' + (i + 1)] = arguments[i];
  }
  console.log(vars.$1); // 12
  return vars;
}

var b = a(12, 34, "a");
console.log(b.$1); // 12
console.log(b.$2); // 34
console.log(b.$3); // "a"
Community
  • 1
  • 1
pzmarzly
  • 778
  • 15
  • 29