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?

fouldsy
  • 3
  • 3
  • 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. – Shadowfool Jun 20 '17 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`](https://nodejs.org/api/globals.html#globals_global). – Heretic Monkey Jun 20 '17 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 – RizkiDPrast Jun 20 '17 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. – fouldsy Jun 20 '17 at 22:41

1 Answers1

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]);
}
Hugo Zapata
  • 4,145
  • 5
  • 27
  • 30
  • Wonderful, thanks. Setting `var self = this` allowed me to access within the function. Now I can go research why this is (still learning). Thanks. – fouldsy Jun 20 '17 at 22:39
  • This appears to be a good starter for understanding why this works - https://stackoverflow.com/questions/962033/what-underlies-this-javascript-idiom-var-self-this. Thanks again. – fouldsy Jun 20 '17 at 22:42
  • you can also try `window[toCycle]` – Logan Murphy Jun 20 '17 at 22:46