I have a function like:
define(['module', 'controller'], function(module, controller){
(new module).build();
});
inside module.build
I want to get the arguments automatically of the parent like:
module = function(){
this.build = function(args){
// make args the arguments from caller ( define ) fn above
};
};
I know I could do something like:
module.build.apply(this, arguments);
but I was wondering if there was a better way. Any thoughts?
I have a function like:
define(['module', 'controller'], function(module, controller){
(new module).build();
});
inside module.build
I want to get the arguments automatically of the parent like:
module = function(){
this.build = function(args){
// make args the arguments from caller ( define ) fn above
};
};
I know I could do something like:
module.build.apply(this, arguments);
but I was wondering if there was a better way. Any thoughts?
Share Improve this question edited Nov 6, 2014 at 14:05 axelduch 10.8k2 gold badges33 silver badges50 bronze badges asked Nov 6, 2014 at 13:41 amcdnlamcdnl 8,65813 gold badges64 silver badges104 bronze badges 4-
I'm not sure I understand your use of
define()
. Can't find it on MDN. Are you using requireJS? If so, could you add that tag? Re: your question, I'd like to know as well. My thoughts are that perhaps there's something to do with creating Classes, but I'm relatively weak on my understanding of all that Classes can do. Maybe along the lines ofmodule = new myClass; module.define(args); module.build();
? – Phil Tune Commented Nov 6, 2014 at 13:51 -
Does the code above work? Because it seems weird to me that you call
module.build()
which isundefined
since it is the constructor that adds the build method – axelduch Commented Nov 6, 2014 at 13:55 - @aduch whoops, i was prototyping some example code. Fixed now – amcdnl Commented Nov 6, 2014 at 14:04
- @amcdnl I fixed it for you – axelduch Commented Nov 6, 2014 at 14:06
1 Answer
Reset to default 8There is a way to do this, illustrated in this example (http://jsfiddle/zqwhmo7j/):
function one(){
two()
}
function two(){
console.log(two.caller.arguments)
}
one('dude')
It is non-standard however and may not work in all browsers:
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller
You would have to change your function like so:
module = function(){
this.build = function build(args){
// make args the arguments from caller ( define ) fn above
console.log(build.caller.arguments)
};
};