JavaScript get parent function arguments

I have a function like:

define(['module', 'controller'], function(module, controller){ (new module).build(); }); 

inside module.build I want to get the arguments of an automatically parent type:

 module = function(){ this.build = function(args){ // make args the arguments from caller ( define ) fn above }; }; 

I know I can do something like:

 module.build.apply(this, arguments); 

but I was wondering if there is a better way. Any thoughts?

+6
source share
1 answer

There is a way to do this, illustrated in this example ( http://jsfiddle.net/zqwhmo7j/ ):

 function one(){ two() } function two(){ console.log(two.caller.arguments) } one('dude') 

However, it is non-standard and may not work in all browsers:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller

You would need to change your function as follows:

 module = function(){ this.build = function build(args){ // make args the arguments from caller ( define ) fn above console.log(build.caller.arguments) }; }; 
+7
source

Source: https://habr.com/ru/post/977833/


All Articles