Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 找出实例变量名本身_Javascript_Oop_Function - Fatal编程技术网

Javascript 找出实例变量名本身

Javascript 找出实例变量名本身,javascript,oop,function,Javascript,Oop,Function,我创建了一个JavaScript类,如下所示: function MyClass() { this.myProp = ''; } MyClass.prototype.myTestFunction = function() { alert('test'); } 现在,我实例化这个类 var myTestInstance = new MyClass(); myTestInstance.myTestFunction(); 这将输出带有“测试”的警报 现在,我希望将变量名“myTes

我创建了一个JavaScript类,如下所示:

function MyClass() {
    this.myProp = '';
}
MyClass.prototype.myTestFunction = function() {
    alert('test');
}
现在,我实例化这个类

var myTestInstance = new MyClass();
myTestInstance.myTestFunction();
这将输出带有“测试”的警报

现在,我希望将变量名“myTestInstance”放入函数“myTestFunction()”中,而不必将其作为参数传递

是否有可能从被调用函数内部找到实例的变量名

谢谢你的帮助

编辑:只是为了添加我需要的信息:我在实际项目中创建的每个实例都是一个特殊的HTML表。标题字段中是每列的排序按钮。因此,我使用
href='javascript:myTableInstance.sort()'
动态添加了一个link元素。要在实例中动态打印,我需要变量名


还有其他更好的解决方案吗?

不,这毫无意义。首先,实例与单个变量无关(它可能被许多变量引用,也可能不被任何变量引用——可能是某个数组的成员)——因此“存储实例的变量名是什么”这个问题无法回答。其次,myTestFunction和myTestInstance的范围可能会有很大的不同。在通常情况下,myTestFunction不会“看到”定义了myTestInstance的范围,因此知道变量的名称没有帮助


您应该在myTestFunction中使用“this”

不,这毫无意义。首先,实例与单个变量无关(它可能被许多变量引用,也可能不被任何变量引用——可能是某个数组的成员)——因此“存储实例的变量名是什么”这个问题无法回答。其次,myTestFunction和myTestInstance的范围可能会有很大的不同。在通常情况下,myTestFunction不会“看到”定义了myTestInstance的范围,因此知道变量的名称没有帮助

您应该在myTestFunction中使用“this”