简体   繁体   中英

Kinetic.js / Javascript: calling a variable as a property without using eval()?

I am trying to teach myself HTML5 and JavaScript, and would appreciate any help. I solved a problem by using eval(), and was wondering if there was another way to solve it without using eval(). Everything I've tried doesn't seem to work.

I have two arrays, defined like this:

var europe = {  
    shapes: {
    AL: "M520.651,114.27l-0.257,0.900l0.385,1.160l1.029,0.643l0,0.644l-0.901,0.386l-0.128,0.901l-1.288,1.287l-0.386-0.128l-0.127-0.644l-1.417-0.900l-0.259-1.288l0.259-1.803l0.256-0.901l-0.384-0.386l-0.258-0.901l1.287-1.288l0.129,0.516l0.771-0.258l0.516,0.773l0.643,0.257l-0.130-1.030z",
    AM: "M582.697,116.33l3.605,-0.515l0.642,0.772l1.032,0.386l-0.516,0.773l1.416,0.900l-0.772,0.902l1.159,0.643l1.158,0.516l0.129,1.801l-1.029,0.129l-1.032,-1.544l0,-0.515l-1.287,0.129l-0.771,-0.772l-0.516,0l-1.029,-0.773l-2.059,-0.643l0.256,-1.288l0.386,0.901z",
    AT: "M510.996,97.278l-0.257,1.158l-1.545,0l0.643,0.643l-0.900,1.674l-0.515,0.515l-2.446,0l-1.289,0.644l-2.315-0.258l-3.734-0.644l-0.644-0.900l-2.703,0.386l-0.258,0.514l-1.672-0.386l-1.416,0l-1.160-0.514l0.385-0.644l-0.128-0.515l0.903-0.128l1.285,0.772l0.387-0.772l2.446,0.128l1.931-0.515l1.287,0.128l0.773,0.515l0.258-0.386l-0.387-1.802l1.030-0.386l0.901-1.158l2.058,0.772l1.417-1.030l1.030-0.258l2.061,0.901l1.286-0.129l1.158,0.516l-0.127,0.256l-0.257-0.903z",
    },
    names: {
    AL: "Albania",
    AM: "Armenia",
    AT: "Austria",
    }
};

And what I am doing is iterating over the path data in europe.shapes to draw it, and then displaying the matching data in europe.names using this code:

for(var euroKey in europe.shapes) {
    var pathEuro = new Kinetic.Path({
      data: europe.shapes[euroKey],
      name: euroKey,
      fill: 'yellow',
      stroke: '#555',
      strokeWidth: 1
    });

    pathEuro.on('mouseover', function() {
      var euroText = eval('europe.names.'+this.getName());
      writeMessage(euroText);
      this.setFill('#CCCCCC');
      this.moveTo(topLayer);
      topLayer.drawScene();
    }); 

    pathEuro.on('mouseout', function() {
      writeMessage('');
      this.setFill('#eee');
      this.moveTo(mapLayer);
      topLayer.draw();
    });

    textLayer.add(countryText);
    mapLayer.add(pathEuro);
};

This is mostly stolen from the tutorial here: http://www.html5canvastutorials.com/labs/html5-canvas-world-map-svg-path-with-kineticjs/ but I am trying to add in the mouseover text of the country name. This method using eval() works, but as I doing this on my own I don't want to learn bad habits.

The whole code is up on GitHub here: https://github.com/malkie-labs/html5-world-map if anyone is really, really bored and wants to take a look. Any constructive criticism or suggestions are appreciated!

Thanks! -Scott

If you are only using eval to access variable name object members, you can do it w/o eval:

eval('europe.names.'+this.getName());

simply becomes

europe.names[this.getName()];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM