0

Let's say I have a function in a namespace like:

app.MyNamespace = {
    defaults: {
        myOption: 1
    },
    myFunction(param1, param2, options){
         options = $.extend(options, this.defaults);
    }
}

then if I call this in some other context (e.g. window) with

app.MyNamespace.myFunction.call(this);

obviusly the options does not extend with the defaults because it loose the right context.

How can I mantain the defaults in the function, but calling it in another context?

Fiddle

5
  • app.MyNamespace.myFunction.call(this); why would you call it like that? Commented Dec 9, 2014 at 16:08
  • because it can be used by different classes, it's kinda of en.wikipedia.org/wiki/Delegation_pattern Commented Dec 9, 2014 at 16:09
  • you can use bind In your myFunction you add .bind(this); Commented Dec 9, 2014 at 16:12
  • maybe you did not get my point :) Commented Dec 9, 2014 at 16:19
  • Oh ok.. so you want to change the context. Thought the problem was maintain the context. Commented Dec 9, 2014 at 16:26

2 Answers 2

2

just point it to defaults

app.MyNamespace = {
    defaults: {
        myOption: 1
    },
    myFunction(param1, param2, options){
         options = $.extend(options, app.MyNamespace.defaults);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

if i get what you want, this should do:

app.MyNamespace = {
defaults: {
    myOption: 1
},
myFunction:function(param1, param2, options, scope){
     options = $.extend(options, this.defaults);
 }}; app.MyNamespace.myFunction('param1','param2', {option1:1,option2:2}, this);

Comments

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.