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

207
Visualizações
How to get the prototype name inside the function itself?

I'm creating a function for Element and I couldn't find a proper way to get the prototype name itself into it, that's the way I tried, but I don't think it's the most appropriate way, there is some more conventional way to to get the name inside the prototype itself?


Element.prototype.getCssStyle = function getCssStyle(stylePropertyName) {
    var protoName = Element.getCssStyle.name;
 
    return protoName;
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

arguments.callee ref to the function itself.

Element.prototype.getCssStyle = function getCssStyle(stylePropertyName) {
    var protoName = arguments.callee.name;

    return protoName;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If your goal is to get the string "getCssStyle"within thegetCssStylefunction, you'd do that by usinggetCssStyle.name`:

Element.prototype.getCssStyle = function getCssStyle(stylePropertyName) {
    var functionName = getCssStyle.name;
 
    return functionName;
}
console.log(document.createElement("div").getCssStyle());

The name of a function provides an in-scope identifier within the function that refers to the function, and functions have a name property giving their name.

Or get it from the same place you assigned it, Element.prototype.getCssStyle:

Element.prototype.getCssStyle = function getCssStyle(stylePropertyName) {
    var functionName = Element.prototype.getCssStyle.name;
 
    return functionName;
}
console.log(document.createElement("div").getCssStyle());

Note that in both cases, this is the name of the function, not necessarily the property you assign it to.


Side note: I strongly recommend not creating methods on built-in prototypes via direct assignment. It creates enumerable properties on the prototypes, which can mess up naively-written code.

It's generally best not to add to built-in prototypes at all (and certainly not in library code), but it can be okay-ish in your own app or page code. To do it, use Object.defineProperty

Object.defineProperty(Element.prototype, "getCssStyle", {
    value: function getCssStyle(stylePropertyName) {
        var functionName = getCssStyle.name;
 
        return functionName;
    },
    enumerable: false,
    configurable: true,
    writable: true,
});
console.log(document.createElement("div").getCssStyle());

The enumerable flag is false by default (all three flags are), but I've included it above for emphasis.

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