0

I have 2 JS files - one with the functions I would like to access and the other that I'd like to call the function with.

(function($) {
    var Place = function() {
        var Location = function(id, duration, check) {
           //Should access this function
        }
    }
})(jQuery);

I'm trying to access it with:

Place.Location(markerId, 600);

But all I'm getting is that it's not defined. Simple issue but can't quite figure this one out.

As it's a jQuery plugin, maybe there's a way I can access it via another method?

$.fn.place = function(params) {
    var len = this.length;

    return this.each(function(index) {
        var me = $(this),
            key = 'place' + (len > 1 ? '-' + ++index : ''),
            instance = (new Place).init(me, params);
    });
};

3 Answers 3

1

The way you are defining Location, it is a private variable inside the function Place. If you want to access it as an attribute of Place, you should replace var Location = ... with this.Location = ...

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but then Location is not defined.
0

It's going out of scope. Because you wrapped your Place object in function($) {}, now anything outside that wrapper will no longer have access to variables inside the wrapper. If $ stands for jQuery, it should be a global anyways and you can take the wrapper out.

9 Comments

Thanks David. So if I remove the outside wrapper would I then be able to use Place.Location()?
Yep! You can pass any parameter you need as well directly by doing var Place = function(*arguments*) {};
Thanks again. I've removed the outer function and then tried to access it with Place.Location(*args*) but it's still saying the same thing.
Are you initializing your Place object? Location will not be initialized unless you have something like var myPlace = new Place();, where the Location function will now be under myPlace.Location(). This is because Place will not run any of its inner code unless it is called, and Place will never be initialized.
Yep, I'm initializing it and still the same thing, "undefined...".
|
0

The solution is a combination of the other two answers.

  1. You define Place as a variable in the (anonymous) function. It can't be used outside the scope of that function. (This function doesn't use jQuery, either, so the wrapper is unnecessary).
  2. Place is a function. It executes code that sets local variable Location to a function, but doesn't export that function, so Location() is inaccessible outside the Place function.

You probably mean to make Place an object (instead of a function), and give it a Location method. Here's one way to write it:

var Place = {
    Location: function(id, duration, check) {
        // do something with id, duration, & check
    }
};

// execute
Place.Location(someId, someDuration, someCheck);

(It doesn't look like you've posted all your code, like the Place.init() method, but there are plenty of ways to write this so that it works correctly; this should solve your immediate problem.)

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.