如何在函数本身中获取原型名称?
How to get the prototype name inside the function itself?
我正在为 Element
创建一个函数,但我找不到将原型名称本身放入其中的正确方法,这是我尝试过的方法,但我认为这不是最有效的方法适当的方式,有一些更传统的方式来获取原型本身内部的名称?
Element.prototype.getCssStyle = function getCssStyle(stylePropertyName) {
var protoName = Element.getCssStyle.name;
return protoName;
}
arguments.callee 引用函数本身。
Element.prototype.getCssStyle = function getCssStyle(stylePropertyName) {
var protoName = arguments.callee.name;
return protoName;
}
如果您的目标是获取字符串“getCssStyle”within the
getCssStylefunction, you'd do that by using
getCssStyle.name`:
Element.prototype.getCssStyle = function getCssStyle(stylePropertyName) {
var functionName = getCssStyle.name;
return functionName;
}
console.log(document.createElement("div").getCssStyle());
函数的名称在函数内提供了引用该函数的范围内标识符,并且函数有一个 name
属性 给出它们的名称。
或者从您分配它的同一个地方获取它,Element.prototype.getCssStyle
:
Element.prototype.getCssStyle = function getCssStyle(stylePropertyName) {
var functionName = Element.prototype.getCssStyle.name;
return functionName;
}
console.log(document.createElement("div").getCssStyle());
请注意,在这两种情况下,这是 函数 的名称,不一定是您分配给它的 属性。
旁注:我强烈建议不要通过直接赋值在内置原型上创建方法。它在原型上创建可枚举的属性,这可能会弄乱天真的代码。
通常最好根本不要添加到内置原型(当然不要添加到 库 代码中),但在您自己的应用程序或页面中也可以代码。为此,请使用 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());
默认情况下 enumerable
标志为 false(所有三个标志都是),但为了强调,我在上面包含了它。
我正在为 Element
创建一个函数,但我找不到将原型名称本身放入其中的正确方法,这是我尝试过的方法,但我认为这不是最有效的方法适当的方式,有一些更传统的方式来获取原型本身内部的名称?
Element.prototype.getCssStyle = function getCssStyle(stylePropertyName) {
var protoName = Element.getCssStyle.name;
return protoName;
}
arguments.callee 引用函数本身。
Element.prototype.getCssStyle = function getCssStyle(stylePropertyName) {
var protoName = arguments.callee.name;
return protoName;
}
如果您的目标是获取字符串“getCssStyle”within the
getCssStylefunction, you'd do that by using
getCssStyle.name`:
Element.prototype.getCssStyle = function getCssStyle(stylePropertyName) {
var functionName = getCssStyle.name;
return functionName;
}
console.log(document.createElement("div").getCssStyle());
函数的名称在函数内提供了引用该函数的范围内标识符,并且函数有一个 name
属性 给出它们的名称。
或者从您分配它的同一个地方获取它,Element.prototype.getCssStyle
:
Element.prototype.getCssStyle = function getCssStyle(stylePropertyName) {
var functionName = Element.prototype.getCssStyle.name;
return functionName;
}
console.log(document.createElement("div").getCssStyle());
请注意,在这两种情况下,这是 函数 的名称,不一定是您分配给它的 属性。
旁注:我强烈建议不要通过直接赋值在内置原型上创建方法。它在原型上创建可枚举的属性,这可能会弄乱天真的代码。
通常最好根本不要添加到内置原型(当然不要添加到 库 代码中),但在您自己的应用程序或页面中也可以代码。为此,请使用 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());
默认情况下 enumerable
标志为 false(所有三个标志都是),但为了强调,我在上面包含了它。