简体   繁体   中英

Dynamic function name in javascript?

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

The function's name property by default isn't writeable , but since it's configurable we can still use Object.defineProperty to change it. Since Object.defineProperty conveniently returns the object itself, we can write a function with a dynamic name like this:

 const theName = 'foobar' const fn = Object.defineProperty(function () { /*... */ }, 'name', { value: theName }) console.log(fn.name) // Logs foobar

Of course this could be factored out into a helper function:

 const nameFunction = (name, fn) => Object.defineProperty(fn, 'name', { value: name }) const fn = nameFunction('foobar', function () { /*... */ }) console.log(fn.name) // Logs foobar

The above nameFunction function can also be used to rename an existing function, of course (here it's just renaming and returning the anonymous one).

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

Node.js JavaScript Class Based Dynamic Function Name

File: Schema.js

class Schema {
  constructor() {
    this.name = null;
  }

  virtual(name = null) {
    this.name = name;
    return this;
  }

  get(func = false) {
    if (!this.name || !func instanceof Function) {
      throw new Error("Name and function must be provided.");
    }
    // Attach the dynamic function name to the "this" Object
    this[this.name] = func;
    this.name = null;
  }
}

module.exports = Schema;

File: index.js

const Schema = require("./Schema.js");

const User = new Schema();

User.virtual("getPostCount").get(() => {
  return 10 + 10;
});

const ok = User.getPostCount();
console.log({ User });
console.log(ok);

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

I have this:

this.f = function instance(){};

I would like to have this:

this.f = function ["instance:" + a](){};

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