skip to Main Content

I see some use:

var dom = document.getElementById('id');
var myChart = echarts.init(dom);

and others:

myChart = echarts.init(document.getElementById('id'));

Is one preferable for some reason or is it just a matter of choice? I prefer the second as it saves a line of code

2

Answers


  1. I think this is a personal preference. Some folks may find it preferable to explicitly declare a variable to get the document’s id. And another to initialize the charts.

    One thing I will mention is that using var is falling out of flavor in post ES6, in favor of let or const.

    Login or Signup to reply.
  2. I would suggest using a const to store the DOM Element for later, as you are already getting it and you might need it later, if you don’t, you could always revert to the original method of getting the DOM Element within the args of the function.

    If you even might need to use the element later

    const dom = document.getElementById('id');
    let myChart = echarts.init(dom);
    

    If you know you don’t need the element again

    let myChart = echarts.init(document.getElementById('id'));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search