0

For example, I have a function defined as below:

var fn1 = function(obj, p1, p2) {}

Is it possible to get this function's parameters by the function name fn1 outside fn1?
I want to make a new fn2(p1, p2) dynamically and in this function passing the obj to call fn1?
Anyone help on this? Many thanks.

3
  • Scope is dangerous territory in Javascript. What are you trying to do? A more concrete example would help. Commented Mar 20, 2016 at 0:57
  • very broad be more specific Commented Mar 20, 2016 at 1:07
  • I change the scope to obj, it is just a object. Commented Mar 20, 2016 at 1:19

1 Answer 1

1

You don't need to pass the scope to the function like this:

var fn1 = function(scope, p1, p2) {}

I belive what you want is something like the call method. The call method is used to call a method on behalf of another object. It allows you to change the this object of a function from the original context to the new object specified by first arg. call Method Ref. - JS

Here some example:

var fn1 = function(p1,p2){};

var fnThatCall = function(p1,p2){
   var scpe = this;
   fn1.call(scope,p1,p2);
};
Sign up to request clarification or add additional context in comments.

2 Comments

yes, you are right, that is my meaning, but I want to define 'fnThatCall' dynamically based on the 'fn1' parameters, not hard coded. possible?
Sure, exists some approaches to do that, so one of them is this: you can use the arguments objects that is a local variable present in all function objects, it is an array with all arguments, so you need to abstract the name of the parameter and use this array. msdn.microsoft.com/en-us/library/87dw3w1k(v=vs.94).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.