[英]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.