javascript, access other members during anonymous object creation?

anonymous, javascript, member, this

Solution

The `data` function is being called before the object is defined; `this` will not be an alias to an object prior to that object's coming into existence. You can use a constructor function with a private, short-lived variable:

var getOverviewData = function () { return "I'm overview data!" }
var chartData = "I'm chart data!"

var store = new function () {
  var _masterData
  this.revert = function() {
    this.data = shallowCopy(this.masterData)
  }
  this.data = (function() {
    _masterData = getOverviewData()
    return chartData
  }).call()
  this.masterData = _masterData
}()

console.log(store)
// { revert: [Function],
//   data: 'I\'m chart data!',
//   masterData: 'I\'m overview data!' }

Problem

Trying to get the following code to work; As the store variable is assigned an anonymous object, the "data" property is populated using a function call. This setting should also set the contents of the other object's property "masterData". I expected the "this" keyword to refer to the anonymous object being created, but I'm wrong... ``` var store = { masterData : [], revert: function() { this.data = shallowCopy(this.masterData); }, data: (function() { var overviewData = getOverviewData(); this.masterData = overviewData; return chartData; }).call(), }; ``` See also that "revert" property; It's given a function that'll create a copy of the object's data property contents. What should be used, since "this" returns the DOMWindow object?

Original source