Java enum est très sophistiqué et vous pouvez créer une énumération avec plusieurs champs et méthodes comme suit.
Java.Color.java
enum Color {
RED("rouge", 255, 0, 0),
YELLOW("Jaune", 255, 255, 0),
GRAY("gris", 155, 155, 155);
Color(String name, int r, int g, int b) {
this.name = name;
this.r = r;
this.g = g;
this.b = b;
}
public String name;
public int r, g, b;
public String toHexCode() {
return String.format("#%02x%02x%02x", r, g, b);
}
}
System.out.println(Color.YELLOW.toHexCode()); // => "#ffff00"
System.out.println(Color.GRAY.name); // => "gris"
Si vous implémentez ceci docilement avec JavaScript, cela ressemblera sûrement à ceci.
JavaScript.color.js
var Color = (function() {
var InnerType = function(name, r, g, b) {
this.name = name;
this.r = r;
this.g = g;
this.b = b;
};
InnerType.prototype.toHexCode = function() {
return '#' + ('00' + this.r.toString(16)).slice(-2) + ('00' + this.g.toString(16)).slice(-2) + ('00' + this.b.toString(16)).slice(-2);
};
return {
RED : new InnerType('rouge', 255, 0, 0),
YELLOW : new InnerType('Jaune', 255, 255, 0),
GRAY : new InnerType('gris', 155, 155, 155)
};
})();
console.log(Color.YELLOW.toHexCode()); // => "#ffff00"
console.log(Color.GRAY.name); // => "gris"
Cela a l'air bien à première vue, mais ... En Java, il est naturel d'utiliser cette énumération comme clé de Map.
Map<Color, Integer> colorNums = new HashMap<>();
colorNums.put(Color.RED, 10);
colorNums.put(Color.YELLOW, 20);
colorNums.put(Color.GRAY, 30);
System.out.println(colorNums.get(Color.RED)); // => "10"
System.out.println(colorNums.get(Color.YELLOW)); // => "20"
Dans la version JavaScript,
var colorNum = {};
colorNum[Color.RED] = 10;
colorNum[Color.YELLOW] = 20;
colorNum[Color.GRAY] = 30;
console.log(colorNum[Color.RED]); // => "30" !!?
console.log(colorNum[Color.YELLOW]); // => "30" !!?
Et il ne se comporte pas comme prévu. Puisque Color.RED, Color.YELLOW et Color.GRAY sont des objets, le toString par défaut de l'objet est appelé lorsqu'il devient la clé du tableau associatif, et tous les trois deviennent «[object Object]». Cause.
Ainsi, si vous essayez de renvoyer une chaîne de caractères unique pour chacun avec toString comme suit, cela fonctionnera comme prévu.
JavaScript.color_fix.js
var Color = (function() {
const InnerType = function(name, r, g, b, toStringValue) {
this.name = name;
this.r = r;
this.g = g;
this.b = b;
this.toString = function() {
return toStringValue;
};
};
InnerType.prototype.toHexCode = function() {
return '#' + ('00' + this.r.toString(16)).slice(-2) + ('00' + this.g.toString(16)).slice(-2) + ('00' + this.b.toString(16)).slice(-2);
};
return {
RED : new InnerType('rouge', 255, 0, 0, 'RED'),
YELLOW : new InnerType('Jaune', 255, 255, 0, 'YELLOW'),
GRAY : new InnerType('gris', 155, 155, 155, 'GRAY')
};
})();
var colorNum = {};
colorNum[Color.RED] = 10;
colorNum[Color.YELLOW] = 20;
colorNum[Color.GRAY] = 30;
console.log(colorNum[Color.RED]); // => "10"
console.log(colorNum[Color.YELLOW]); // => "20"
Je suis heureux.
Cependant, il est un peu décevant de spécifier moi-même la chaîne de caractères pour toString, donc si vous pouvez utiliser ES6, il est préférable de l'automatiser à l'aide de Symbol ().
JavaScript.color_fix_es6.js
const Color = (() => {
const InnerType = function(name, r, g, b) {
this.name = name;
this.r = r;
this.g = g;
this.b = b;
const sym = Symbol();
this.toString = () => sym;
};
InnerType.prototype.toHexCode = function() {
return '#' + ('00' + this.r.toString(16)).slice(-2) + ('00' + this.g.toString(16)).slice(-2) + ('00' + this.b.toString(16)).slice(-2);
};
return {
RED : new InnerType('rouge', 255, 0, 0),
YELLOW : new InnerType('Jaune', 255, 255, 0),
GRAY : new InnerType('gris', 155, 155, 155)
};
})();
L'énumération de Java est la plus puissante, mais la flexibilité de JavaScript qui peut atteindre presque la même chose est bonne.
Recommended Posts