使用Javascript:如何获得一个函数里面的父项? [英] Javascript: How to get the parent key inside a function?
问题描述
我有一些功能,存储在一个集合/数组并想拿到钥匙(函数名),无需重新键入它。有没有访问任何一小段路?
VAR functions_collection = {
function_x:功能(){
变量名称=function_x;
//名称= this.key; < - 如何拿到钥匙/函数名function_x? //一些code,如:
$(。function_x .button)VAL(); 警报(名);
}
}
编辑:我想避免重新输入函数本身和preFER里面的function_x来称呼它this.key
抱歉提前奇特的话题,并感谢!
解决方案:很多很好的答案,但我只是在寻找这个剪断:
Object.keys(本)
要获取的对象键的名称,你可以使用 Object.getOwnPropertyNames(本)
或新的浏览器只是 Object.keys(本)
,这将让你所有的数组和任何键这个
对象:
VAR functions_collection = {
function_x:功能(){
变种名称= Object.keys(本);
的console.log(名);
}
}
I've some functions, stored in a collection/array and would like to get the key (function-name) without retyping it. Is there any short way to access it?
var functions_collection = {
"function_x": function() {
var name = "function_x";
// name = this.key; <- how to get the key/function-name "function_x"?
// some more code like:
$(".function_x .button").val();
alert(name);
}
}
Edit: I'd like to avoid retyping the "function_x" inside the function itself and prefer to call it like this.key.
Sorry for the weird topic and thanks in advance!
Solution: A lot of good answers, but I was just looking for this snipped:
Object.keys(this)
To get the name of the objects keys, you can use Object.getOwnPropertyNames(this)
or in newer browsers just Object.keys(this)
, and that will get you an array of all and any keys the this
object has :
var functions_collection = {
function_x: function() {
var name = Object.keys(this);
console.log(name);
}
}
这篇关于使用Javascript:如何获得一个函数里面的父项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!