0

I'm running in to a problem with object scope, but I 'm struggling to grasp why:

'use strict';

// Build array of equipment, connected pin, and default state
var equipmentData = [{shortName: 'dayLights', longName: 'Day lights', pin: 1, state: true},
    {shortName: 'nightLights', longName: 'Night lights', pin: 2, state: false}];

// Constructor build a new object
var Equipment = function(shortName, longName, pin, state) {
    this.shortName = shortName;
    this.longName = longName;
    this.pin = pin;
    this.state = state;

    console.log('Created object for ' + this.longName);
}

// Loop through the array and create an object for each
for (var i = 0; i < equipmentData.length; i++) {
    var equipmentName = equipmentData[i].shortName;
    this[equipmentName] = new Equipment(equipmentData[i].shortName, equipmentData[i].longName, equipmentData[i].pin, equipmentData[i].state);
}

// Pick what you now want to review
var toCycle = 'dayLights';
console.log(this[toCycle]);

This all works just fine. When I then try move the last couple of lines to output the object but this time within a function as follows:

function inside() {
    var toCycle = 'dayLights';
    console.log(this[toCycle]);
}

inside();

it fails with:

TypeError: Cannot read property 'dayLights' of undefined

Even just:

function inside() {
    console.log(dayLights);
}

inside();

fails in the same way.

How can I access the global object from within the function?

4
  • inside.call(this) instead of inside() in order to preserve your binding of this as the 'global'. However in your second example, I'm not quite sure what you're trying to do as dayLights is not defined anywhere. Commented Jun 20, 2017 at 22:24
  • What is the this that you think you're adding these properties to? If you want it to be the global object, you need to use global. Commented Jun 20, 2017 at 22:27
  • Since your first this refers to window object, what if just just use window instead of this for the second one(inside) -note: this inside inside function refers to the function not window Commented Jun 20, 2017 at 22:38
  • It's looping through the array that's first declared and creates objects based on the shortname of each. If there's a cleaner way, I'm open to suggestions. Essentially I'd have an array, possibly from database ultimately, from which I want to create objects so I can more easily update values during runtime and attach prototype functions. The above works, and using the answer below, is what I was needing. Commented Jun 20, 2017 at 22:41

1 Answer 1

1

If you want to make the context inside the 'inside' function the same as the outer context you can:

inside.bind(this)()

Or:

var self = this
function inside() {
    var toCycle = 'dayLights';
    console.log(self[toCycle]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Wonderful, thanks. Setting var self = this allowed me to access within the function. Now I can go research why this is (still learning). Thanks.
This appears to be a good starter for understanding why this works - stackoverflow.com/questions/962033/…. Thanks again.
you can also try window[toCycle]

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.