How use displayJS in objects or class
ange007 opened this issue · 3 comments
ange007 commented
Tell me please what I do not correctly (use example3)?
<script>
var $ = new DisplayJS( window );
var obj = {};
obj.set = function( )
{
var list = $.range(5);
var average = $.average( list );
var median = $.median( list );
....
$.var( );
};
obj.set( );
</script>
Or it is necessary to use?:
let data = {
list: '',
average: '',
median: ''
}
const $ = new DisplayJS( data );
But how uses displayJS in classes and many objects?
ange007 commented
I partially understood having looked at source codes of library.
Data undertake from the object transferred to displayJS.
It turns out it is necessary for each object class to create a separate copy of displayJS.
class newClass {
constructor( ) {
this.$ = new DisplayJS( this );
}
}
or
class newClass {
constructor( ) {
this.params = { p1: '1', p2: '2' };
this.$ = new DisplayJS( this.params );
}
}
arguiot commented
There is a much more easier way to use DisplayJS in a class, because DisplayJS is actually a class.
class newClass extends DisplayJS {
constructor() {
this.params = { p1: "Hello World" }
this.$ = super(this.params);
}
}
arguiot commented
And, for your initial question, the problem is the context, if you declare variables inside a function or an object, they won’t be global, they will be local. DisplayJS can’t handle local variables, you need to make them global like var myVar
becomes myVar