我正在开发javascript库。
想要动态地在成员函数体上获取实例或类名,而不添加任何属性。
我发现这些代码打印得像这样。
function Foo()
{
this.var1 = 1;
this.var2 = 2;
this.print = function()
{
arguments.callee.prototype; // This shows "Foo.print {}".
}
}
var myFoo = new Foo();
myFoo.print();
但我不知道如何使用myfoo
获取实例Foo
或类名prototype
。有什么办法吗?
那个对象实例意味着什么?
答案 0 :(得分:2)
将this
的引用存储在变量中并在函数中使用它。
function Foo() {
//Stor reference
var _self = this;
this.var1 = 1;
this.print = function() {
alert(_self.var1);
}
}
var myFoo = new Foo();
myFoo.print();