8

excuse the pseudo code, my actual file is much larger:/

I want to call a function (with parameters) from inside a class. However, that function should be passed to the class as a variable.

someObject = {
    itWorked:function(answer){
       alert(answer);
    },

    plugins:{
        somePlugin:function(){

            var callback;
            this.doSomething = doSomething;

            function setCallback(c){
                callback = c;
            }

            function doSomething(){
                 var answer = "hello";
                 [callback](answer); // how do I call this?
            }

        }
    },

    widgets:{
        something:function(){
            var doIt = new someObject();
            doIt.setCallback(someObject.itWorked()); // how do I send this?
            doIt.doSomething();
        }
    }
}

So how would I pass itWorked() to the class? And how would I call that itWorked(answer) function within the class as well as passing a variable to if?

2 Answers 2

3

You will need to change

setCallback = function (c) {callback = c;}

to

this.setCallback =  function (c) {callback = c;}

so the setCallback function will be public.

If you also want to scope the callback, you can call it like this

callback.call(scope, param1, param2);

If you don't know how many parameters, you can call it like this

callback.apply(scope, parameters);

Scope could be any object, even an empty one {} if you want.

By the way, I really like your use of private variables in this example, great work with the javascript. Here is a good way to write your javascript object to help with the initialization and readability

var mynamespace = {};

(function () {
   function MyObject(param1, param2) {
      this.initialize(param1, param2);
   }

   MyObject.prototype = {
      initialize: function (param1, param2) {
          var privateScope = {
              param1: param1,
              param2: param2,
              callback: null
          };

          this.setCallback = function (c) {
              privateScope.callback = c;
          }

          this.doSomething = function () {
              if (privateScope.callback) {
                  privateScope.callback.call();
              }
          }
      }
   }
   mynamespace.MyObject = MyObject;
}());

Then to use it

var obj = new mynamespace.MyObject("value1", "value2");
Sign up to request clarification or add additional context in comments.

7 Comments

thanks :) I'm a bit of a js noob at this level, thanks for your feedback!
Well you are off to a great start, this is well written javascript.
do you know if there is a __construct() equivalent for javascript? So rather than call doIt.setCallback(someObject.itWorked) pass it in at the new someObject() stage?
@ed209, the function someObject is the constructor function. Functions act as constructors when used with new
Check this doc about .apply() and .call()
|
1

Remove the parentheses to pass the function as a variable.

doIt.setCallback( someObject.itWorked );

You can then use the callback as you would any other function.

callback( answer );

2 Comments

Good answer here, I just posted an alternative implementation, but there is nothing wrong with this one.
You should also check if the callback exists, and is an executable function: if (callback && typeof(callback) === "function") {}

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.