Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
Proper way to get an object that has own value AND has sub-values

So there's those colors object configuration, that is something like this

color: {
main: "red",
light: "lightcoral"
dark: "darkred"

I think it is a bit cumbersome the color.main pattern popping up everywhere, instead of simply color. However, the color.variant part makes ton of sense.

So i wonder how can i easily (and if should i?!) create an object (or any object like entity) in Javascript/Typescript, that mix'em up. Returning a value by it self, just like a plain variable/getter, and still have it's variants on nested properties.

I'm not up to touch on prototype and too fancy stuff to make it for real though. But know all possibilies would be nice.

So to illustrate, the final object should act like this:

console.log(color) // red
console.log(color.light) // lightcoral
console.log(color.dark) // darkred
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Generally speaking: no, because javascript isn't strong typed. Consider the following:

var a = redObject;
var b = a;

Now, why b should be the string "red" rather than a reference to the redObject?

This is not true for strong typed OO languages (not every ones anyway). For example in c++ you can define a custom conversion function for any type. So if you have

class Color {
  operator int() const { return 255; }
  operator char*() const { return "red"; }
}
Color a();
int b = a;
char* c = a;

then b would be 255 and c would be "red", bacause the compiler knows which conversion function it should use based on the type of the assigned variable.

However many OO languages have one implicit conversion function for the string type, which in most cases can be overridden. So you could create a custom Color class with a custom toString method to get what you want, but keep in mind that this will only work when converting the object to string, not for every variable assignment:

<span id="xx"></span>
<script>
function Color(main, light, dark) {
    this.main = main;
    this.light = light;
    this.dark = dark;
}

Color.prototype.toString = function() {
    return this.main;
};

var redObject = new Color("red", "lightcoral", "darkred");
console.log(redObject); // console displays: Color {main: "red", light: "lightcoral", dark: "darkred"}
console.log(''+redObject); // console displays: red
console.log(redObject.light); // console displays: lightcoral
console.log(redObject.dark); // console displays: darkred
$('#xx').text(redObject); // the span displays: red
</script>

Another option is to use a function, which give much more control, but I don't recommend it anyway:

function Color(main, light, dark) {
    var result = function() {
        return main;
    };
    result.light = light;
    result.dark = dark;
    return result;
}

var redObject = Color("red", "lightcoral", "darkred");
console.log(redObject()); // console displays: red
console.log(redObject.light); // console displays: lightcoral
console.log(redObject.dark); // console displays: darkred

A better way is to have a color map and use a function only to query light and dark variants:

var colors = {};

function defineColor(main, light, dark) {
    colors[main] = {main: main, light: light, dark: dark};
}

function light(color) {
    return colors[color].light;
}

function dark(color) {
    return colors[color].dark;
}

defineColor("red", "lightcoral", "darkred");

var color = "red";
console.log(color); // console displays: red
console.log(light(color)); // console displays: lightcoral
console.log(dark(color)); // console displays: darkred
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda