L
L
LionG2021-06-12 12:04:51
JavaScript
LionG, 2021-06-12 12:04:51

How to add a mixin with a large nesting to the class so that the this class would be inside the functions?

let users = {foo: {bar: {function baz() {
 this.req()
}}}}

let test = {function test() {
 this.req()
}}

class API{
 req() {
  console.log('req')
 }
}
Object.assign(API.prototype, {users})
new API().users.foo.bar.baz()//this undefined

Object.assign(API.prototype, test)
new API().test()//req


If in the mixin object immediately the functions go and add them to the class, then this works. And if there is a large investment, then no.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2021-06-12
@bingo347

https://developer.mozilla.org/en/docs/Web/JavaScri...
https://developer.mozilla.org/en/docs/Web/JavaScri...
https://developer.mozilla.org/ en/docs/Web/JavaScri...
https://developer.mozilla.org/en/docs/Web/JavaScri...
https://developer.mozilla.org/en/docs/Web/JavaScri...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question