簡體   English   中英

如何在函數內部獲取原型名稱?

[英]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;
}

如果您的目標是within the getCssStyle function, you'd do that by using獲取字符串“getCssStyle” function, you'd do that by using getCssStyle.name` function, you'd do that by using實現:

 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());

請注意,在這兩種情況下,這是function的名稱,不一定是您分配給它的屬性。

暫無
暫無

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

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