0

I can't figure out how to have a javascript function which accepts a parameter correctly.

I can get the function working perfectly if I don't use a input parameter because I can do this:

var x = MyFunction;

But the moment I have to do this

 var x = MyFunction(e);

Then it breaks.

I tried to work around this by setting the input parameter afterwards, but I can't get anything to work. How can I do this?

http://jsfiddle.net/TxMmG/

var MyFunction = function() {

    var otherResult = function() {
        alert("Hi");
    },

        input, objToAlert = function() {
            return input;
        };

    return {
        objToAlert: objToAlert,
        input: input,
        otherResult: otherResult
    }

}();


var e1 = "test";


//var y = MyFunction(e); //this does not work if i add a parameter to function - moment i put parenthesis i get problems
var x = MyFunction;
x.input = e1; //also cant set the input here
x.objToAlert();
x.otherResult(); 

2 Answers 2

2

You put a () after the function definition, so the function is called and MyFunction is actually the object returned by the function, not the function itself.

Do like this:

var MyFunction = function() {
    // ...
};  // No () here
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that your function returns an object. Therefore you're assigning an object to a var y. You can not treat object as a function.

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.