0
function abc(){
    //multiple variables and functions
    a:function(){alert("a")};
}
function test(){
    var k=abc();
    k.a();
}

In the above case, I have a huge function abc() to be assigned to a variable. I want to call the member functions that are visible, like a() from the variable. Is this possible to implement and please give me a sample code if so.

3
  • Wait...what is the "a:function()" part? That doesn't seem to compile, unless it's some ES6 thing I haven't heard about. Commented May 28, 2015 at 13:15
  • @Katana314 it looks like OP has truncated out relevant bits of code (such as most of an Object Literal) Commented May 28, 2015 at 13:16
  • When you invoke abc, what does it return? Commented May 28, 2015 at 13:17

3 Answers 3

7

When you include the parenthesis after your function, you're assigning the result of the function to your variable.

If you want to assign the function itself, just omit the parenthesis:

var k = abc;
k.a();

EDIT

Per @Kuba Wyrostek's answer, and @Pointy's comment, that a() function won't be properly exposed.

You'll need to take a look at the Module Pattern. What you need to do is to assign a function to a variable, and have that function return the functions that you want to be able to use outside of that function. This helps with encapsulation.

It's a little hard to tell from your code in the comment exactly what is the user-generated code, but I'll do my best.

var abc = (function () {
    var grabApi,
        initialize;

    // Just an example of how to assign an existing function
    // to a property that will be exposed.
    grabApi = SCORM2004_GrabAPI();

    // This is an example of how to have a property that will be
    // exposed be a custom function passing a parameter.
    initialize = function(initString) {
        return SCORM2004_GrabAPI().Initialize(initString);
    };

    return {
        getApi: grabApi,
        init: initialize
    }
}());

You can then use this abc object like this throughout your code. Again, this is trying to give an example of how to do what I think you're trying to do based on your comment.

// Assign the SCORM2004_GrabAPI function to a variable.
var SCORM2004_objAPI = abc.getApi();

// Call the Initialize function with an empty string.
abc.init("");
Sign up to request clarification or add additional context in comments.

4 Comments

This is true, but it should be noted that a function called "a" declared locally inside function "abc" will not be available external to "abc" as a property of the function object.
SCORM2004_objAPI=SCORM2004_GrabAPI(); strResult = SCORM2004_objAPI.Initialize(""); This is a software generated code. Here I need write the SCORM2004_GrabAPI() and return the object in such a way that the user can access the SCORM2004_objAPI.Initialize("");. I don't understand how exactly I could implement it.
Thanks for that catch, @Pointy. I didn't think all the way through that. I'll update and put a nod to Kuba's answer as well.
@Whoophee I've updated my answer to include some more detail, and hopefully help you understand how to do what I think you're trying to do. The whole "software generated code" part is throwing me a bit, though I do understand how much more complicated that can make your question. Hopefully this will help you understand better.
1

Hmmm… contrary to @krillgar's answer, I believe you were expecting your abc() to return new object. Something like this:

function abc(){
    var privateVar;
    return {
    //multiple variables and functions
    a:function(){alert("a")}
    }
}
function test(){
    var k=abc();
    k.a();
}

Comments

0

You should make it an object. In this way you can access its property a.

var abc ={
    a:function(){alert("a")}
}
function test(){
    var k=abc;//Ofcrse remove the parenthesis 
    k.a();
}
test();

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.