1
Constructor1=function(){};
Constructor1.prototype.function1=function this_function()
{
// Suppose this function was called by the lines after this code block
// this_function - this function
// this - the object that this function was called from, i.e. object1
// ??? - the prototype that this function is in, i.e. Constructor1.prototype
}
Constructor2=function(){};
Constructor2.prototype=Object.create(Constructor1.prototype);
Constructor2.prototype.constructor=Constructor2;
object1=new Constructor2();
object1.function1();
如何在不知道构造函数名称的情况下检索最后一个引用(用???表示)?例如,假设我有一个从原型链继承的对象。当我调用一个方法时,我可以知道使用哪个原型吗?JavaScript函数可以引用原型链上的原型吗?
这两个看起来理论上可能,但我找不到任何工作没有超过常数的赋值语句(如果我有很多这样的功能)。
看到我上面的编辑...我澄清了一点 – user1537366 2012-07-19 10:47:32
@ user1537366:看到我的更新。 – 2012-07-19 10:58:56
嗯,这有点像做浏览器已经做的事情,所以我很惊讶没有更好的办法......另外,你将如何找出我最初想要的是什么?是这样吗? 'get_prototype_containing(this,this_function.toString()。substring(“function”.length,this_function.toString()。indexOf(“(”)))? – user1537366 2012-07-19 11:32:40