[英]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](){};
文件: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.