Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!