-2

In below code I can access wrapper like this: tutorial.screen1.wrapper

var tutorial = {
  screen1: {
    text: '<div class="text">Click this to continue</div>',
    wrapper: '<div class="tutorial tutorial-screen-1">' + this.text + '</div>'
  }

Because this.text cannot be accessed, tutorial.screen1.wrapper throws error.

How to make this this.text work with in the object?

Syed
  • 15,657
  • 13
  • 120
  • 154
  • Im not 100% sure, but I think he is meaning `this.text` as `tutorial.screen1.text`. ie he wants to use the value of `tutorial.screen1.text` in `tutorial.screen1.wrapper`, to get something like `wrapper: '
    ' + '
    Click this to continue
    ' + '
    '`
    – BigSpicyPotato Oct 15 '17 at 07:08
  • @Krusader that won't work, since you are referencing the tutorial object before it is defined – BigSpicyPotato Oct 15 '17 at 07:10
  • In your example, `this` refers to `window`. – frogatto Oct 15 '17 at 07:46

1 Answers1

1

You can do like this by creating wrapper as function

var tutorial = {
  screen1: {
    text: '<div class="text">Click this to continue</div>',
    wrapper: function() {
      return '<div class="tutorial tutorial-screen-1">' + this.text + '</div>'

    }
  }
}
console.log(tutorial.screen1.wrapper())
brk
  • 48,835
  • 10
  • 56
  • 78