简体   繁体   中英

Accessing a variable within a JavaScript object constructed via a function

My application is accessing a third party external JavaScript file that I cannot alter.

Within the file is an object defined similarly to as follows:

object_1 = (function(){
    var object_1_var = 'object_1_var_value';
    return {
        obj_1_func: function() {
            console.log(object_1_var);
        }
    }
})(window);

I need to be able access the object_1_var within the object, but I'm struggling to access it.

object_1.v // returns undefined
object_1.obj_1_func() // returns the value via console, but I need to assign it to a var.

I have tried extending the object using as follows: (Using jQuerys $.extend())

object_2 = (function(){
    return {
        obj_2_func: function() {
            return object_1_var;
        }
    }
})(window);

$.extend(object_1, object_2);

var my_var = object_1.obj_2_func(); // returns 'Uncaught ReferenceError: object_1_var is not defined'

What can I do to be able to access object_1_var ?

You will not be able to access the variable. It happens to be a private member. Private members of an object can be accessed only by its member functions.

Read this .

Make it public, like this:

object_1 = (function(){
 var object_1_var = 'object_1_var_value';
 return {
    obj_1_func: function() {
        console.log(object_1_var);
    },
    object_1_var: object_1_var
 }
})(window);

EDIT

If unable to edit the javascript (such as in a third party library - sorry for omission) then you will not be able to have access to object_1_var as it's scope is local to the closure object_1 .

object_1_var is a lexically scoped local variable.

That means that it can't be accessed by extending object_1 outside of its original definition.

The only way it can be accessed is by adding functions within the original lexical scope in which it was declared:

object_1 = (function(){
    var object_1_var = 'object_1_var_value';
    return {
        obj_1_func: function() {
            console.log(object_1_var);
        }
        var_1: function(x) {
            if (typeof x !== 'undefined') {
                object_1_var = x;
            } else {
                return object_1_var;
            }
        }
    }
})(window);

but since you can't modify object_1 , you're out of luck, I'm afraid!

Sadly, object_1_var isn't accessible in this example. The variable is defined as local to within that particular function - the only reason that the other functions can see it is because they are also defined within that function. This "closure scoping" is an interesting feature in JavaScript that you don't see very often elsewhere, but is the only real way of defining "private" variables in JavaScript Objects.

Hope that helps!


In a worst case scenario, in the past I've worked around this sort of issue by effectively overwriting the definition of an object that was previously defined elsewhere - mainly in Greasemonkey scripts - but I wouldn't condone this for production uses!

The trick here is to just copy the entire piece of script into your own. It's ugly as hell, but it might just work! (YMMV)

What you are trying to accomplish is impossible in JS.

With the construction of object_1 the variable goes out of scope of that method. The reason why the logging function can access the variable is what we call 'a closure'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM