kuhnza / angular-mixpanel

Mixpanel module for Angular JS
MIT License
54 stars 29 forks source link

Understanding function callMixpanelFn(name) in your code #1

Closed mehtaad closed 9 years ago

mehtaad commented 10 years ago

Hi, I was trying to understand your below code function callMixpanelFn(name) { return function () { var fn = window.mixpanel, parts = name.split('.'), scope, i;

            for (i = 0; i < parts.length; i++) {
                scope = fn;
                fn = fn[parts[i]];
            }

            return fn.apply(scope, arguments);
        }

I am not able to understand from were arguments parameter will get filled? you are not passing any parameters to function call except name. Is their any chance to update a working demo code with a call to mixpanel api with arguments? For example how would below function execute if I use your wrapper in my code? mixpanel.track("signup", { "age": 28, "gender": "male", "source": "facebook" });

Thanks, Ashok

kuhnza commented 10 years ago

JavaScript has a special keyword called arguments which provides access to an array-like object containing all arguments that were passed to a function. Importantly arguments contains all arguments passed to a function even if the function doesn’t declare any parameters. You can read more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

In the code you’ve referenced I’m doing a funky little bit of meta-programming which calls a function using the Function#apply method. The apply method takes two parameters, the scope to execute the function against and an array of arguments. I’m simply passing through whatever arguments were passed to function dynamically. You can read more about apply here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

So the short of it is just call the methods on the $mixpanel service exactly as you would on the window.mixpanel object and all should just work as expected.

--  Dave Kuhn

AU: +61 410 817 855 US: +1 415 799 9234

@davekuhn

http://au.linkedin.com/in/davidskuhn

On Mon, Nov 3, 2014 at 12:37 AM, mehtaad notifications@github.com wrote:

Hi, I was trying to understand your below code function callMixpanelFn(name) { return function () { var fn = window.mixpanel, parts = name.split('.'), scope, i; for (i = 0; i < parts.length; i++) { scope = fn; fn = fn[parts[i]]; } return fn.apply(scope, arguments); } I am not able to understand from were arguments parameter will get filled? you are not passing any parameters to function call except name. Is their any chance to update a working demo code with a call to mixpanel api with arguments? For example how would below function execute if I use your wrapper in my code? mixpanel.track("signup", { "age": 28, "gender": "male", "source": "facebook" }); Thanks,

Ashok

Reply to this email directly or view it on GitHub: https://github.com/kuhnza/angular-mixpanel/issues/1

mehtaad commented 10 years ago

Thanks Dave. I will try it out.

  From: Dave Kuhn <notifications@github.com>

To: kuhnza/angular-mixpanel angular-mixpanel@noreply.github.com Cc: mehtaad mehtaad@yahoo.com Sent: Monday, November 3, 2014 5:47 AM Subject: Re: [angular-mixpanel] Understanding function callMixpanelFn(name) in your code (#1)

JavaScript has a special keyword called arguments which provides access to an array-like object containing all arguments that were passed to a function. Importantly arguments contains all arguments passed to a function even if the function doesn’t declare any parameters. You can read more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

In the code you’ve referenced I’m doing a funky little bit of meta-programming which calls a function using the Function#apply method. The apply method takes two parameters, the scope to execute the function against and an array of arguments. I’m simply passing through whatever arguments were passed to function dynamically. You can read more about apply here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

So the short of it is just call the methods on the $mixpanel service exactly as you would on the window.mixpanel object and all should just work as expected.

--  Dave Kuhn

AU: +61 410 817 855 US: +1 415 799 9234

@davekuhn

http://au.linkedin.com/in/davidskuhn

On Mon, Nov 3, 2014 at 12:37 AM, mehtaad notifications@github.com wrote:

Hi, I was trying to understand your below code function callMixpanelFn(name) { return function () { var fn = window.mixpanel, parts = name.split('.'), scope, i; for (i = 0; i < parts.length; i++) { scope = fn; fn = fn[parts[i]]; } return fn.apply(scope, arguments); } I am not able to understand from were arguments parameter will get filled? you are not passing any parameters to function call except name. Is their any chance to update a working demo code with a call to mixpanel api with arguments? For example how would below function execute if I use your wrapper in my code? mixpanel.track("signup", { "age": 28, "gender": "male", "source": "facebook" }); Thanks,

Ashok

Reply to this email directly or view it on GitHub: https://github.com/kuhnza/angular-mixpanel/issues/1— Reply to this email directly or view it on GitHub.