R
R
Ruslan Timurziev2017-08-10 10:15:37
css
Ruslan Timurziev, 2017-08-10 10:15:37

How to make a property access through this inside the config of a new instance created on the basis of a class?

My instance has a VIEW property which is currently a function inside the object-argument(conf) of the new instance of MyClass. Now everything works, but how to make VIEW become a string (or object) and be declared as
VIEW: `${this.DATA.title}`
while this returns the conf area where this.DATA.title is equal to ' Title'. In this case, this returns Window.

class MyClass {
  constructor(conf) {
  	/** public props */
  	this.ENTRY = conf.ENTRY;
  	this.DATA = conf.DATA;

  	/** instance methods */
  	// get dom element
  	conf.$ = (el) => {
    var all = document.querySelectorAll(el);
    return all.length > 1 ? all : document.querySelector(el);
  }

  	/** inits */
  	conf.$(this.ENTRY).innerHTML = conf.VIEW();
  	conf.LOGIC();
  }

  die() {
  	conf.$(this.ENTRY).innerHTML = '';
  }
}






var app = new MyClass({
  ENTRY: '#app',

  DATA: {
    title: 'Title'
  },

  VIEW() {
    return `
      <h1>${this.DATA.title}</h1>
    `
  },

  LOGIC() {
    console.log(this.$('h1'))
  },
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
ReActor Dmitry Vershansky, 2019-03-27
@VoisHunter

.menu {
position: fixed;
top: 0;
}

S
Stalker_RED, 2017-08-10
@Stalker_RED

and declared as
VIEW: `${this.DATA.title}`
Immediately at the time of the declaration there is a line. A primitive one that no longer refers to any this. Referenced at the time of the announcement, but as soon as it became a string - that's it.
bind , call , apply .
Choose what suits you best.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question