簡體   English   中英

javascript 中的動態 function 名稱?

[英]Dynamic function name in javascript?

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

默認情況下函數的name屬性是不可writeable的,但由於它是可configurable的,我們仍然可以使用Object.defineProperty來更改它。 由於Object.defineProperty方便地返回 object 本身,我們可以編寫一個 function ,其動態名稱如下:

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

當然,這可以分解為輔助 function:

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

上面的nameFunction function當然也可以用來重命名一個已有的function(這里只是重命名並返回匿名的)。

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

Node.js JavaScript Class 基於動態 Function

文件: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;

文件: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);

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

我有這個:

this.f = function instance(){};

我想要這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM