JS:有没有办法告诉什么对象叫做函数?

时间:2022-09-06 00:25:38

In a function is there a way to get a reference to the object that called it? I have the same instance of a Flash object on the page twice, each one can make calls to JS through ExternalInterface, I cannot code the Flash objects to each pass a different ID because it is 2 instances of the same Flash object, so it there a way for JS to get a reference to which one called the function?

在函数中有一种方法来获取对调用它的对象的引用吗?我在页面上有一个Flash对象的相同实例两次,每个都可以通过ExternalInterface调用JS,我不能编写Flash对象,每次传递一个不同的ID,因为它是同一个Flash对象的2个实例,所以它在那里JS获取对哪一个函数的引用的一种方法?

Thanks!

4 个解决方案

#1


Can't you pass an ID to the Flash object when you instantiate it? (via a query string or params). Then you could use that ID in your JavaScript function calls.

实例化时,无法将ID传递给Flash对象吗? (通过查询字符串或参数)。然后,您可以在JavaScript函数调用中使用该ID。

#2


I don't know about ExternalInterface but have you tried examine the this object during the execution of your function?

我不知道ExternalInterface但您是否尝试在执行函数期间检查此对象?

Of course you could always use a closure. Ultimately you have to give the Flash object a function to be executed. For example I have myObj1 and myObj2 that take a callback method fnCallback but for some reason does not set the this context when executing this functions to themselves. Hence I can do this:-

当然你总是可以使用一个闭包。最终,您必须为Flash对象提供要执行的功能。例如,我有myObj1和myObj2采用回调方法fnCallback但由于某种原因在执行此函数时不设置此上下文。因此,我可以这样做: -

function setCallback(obj, fn)
{
  obj.callback = function() {fn.apply(obj, arguments);}
}

setCallback(myObj1, fnCallback);
setCallback(myObj2, fnCallback);

Now I can code fnCallback using this as a reference to the specific object that is calling the function.

现在我可以使用它作为对调用函数的特定对象的引用来编写fnCallback。

#3


arguments.caller or Function.caller but you might want to look at reorganizing your code to avoid using them. There really is another way to get what you want -- I guarantee it.

arguments.caller或Function.caller但您可能希望重新组织代码以避免使用它们。真的有另一种方法来获得你想要的东西 - 我保证。

Edit: AnthonyWJones pointed out that you're actually looking for the this operator.

编辑:AnthonyWJones指出你实际上正在寻找这个运营商。

The this keyword refers to the context object (a.k.a. current object). In general, in a method, this refers to the calling object.

this关键字引用上下文对象(a.k.a。当前对象)。通常,在方法中,这指的是调用对象。

#4


If you are looking for a way to find a non-function element that called a function, you might consider using the event. This is a pretty handy function I use a lot when passed an event object.

如果您正在寻找一种方法来查找调用函数的非函数元素,您可以考虑使用该事件。这是一个非常方便的函数,我在传递一个事件对象时经常使用它。

function getTargetElement(evt) {
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  var elem;
  if (evt.target) {
      elem = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target;
  } else {
      elem = evt.srcElement;
  }
  return elem;
}

So, for example, if you call a function "find(event);" from a clicked element, find can be coded to use this method to get the event object:

所以,例如,如果你调用一个函数“find(event);”从一个被点击的元素中,find可以编码为使用此方法来获取事件对象:

function find(evt){
  var clickedObj = getTargetElement(evt);
  alert(clickedObj.tagName); //alerts "TD"
}

#1


Can't you pass an ID to the Flash object when you instantiate it? (via a query string or params). Then you could use that ID in your JavaScript function calls.

实例化时,无法将ID传递给Flash对象吗? (通过查询字符串或参数)。然后,您可以在JavaScript函数调用中使用该ID。

#2


I don't know about ExternalInterface but have you tried examine the this object during the execution of your function?

我不知道ExternalInterface但您是否尝试在执行函数期间检查此对象?

Of course you could always use a closure. Ultimately you have to give the Flash object a function to be executed. For example I have myObj1 and myObj2 that take a callback method fnCallback but for some reason does not set the this context when executing this functions to themselves. Hence I can do this:-

当然你总是可以使用一个闭包。最终,您必须为Flash对象提供要执行的功能。例如,我有myObj1和myObj2采用回调方法fnCallback但由于某种原因在执行此函数时不设置此上下文。因此,我可以这样做: -

function setCallback(obj, fn)
{
  obj.callback = function() {fn.apply(obj, arguments);}
}

setCallback(myObj1, fnCallback);
setCallback(myObj2, fnCallback);

Now I can code fnCallback using this as a reference to the specific object that is calling the function.

现在我可以使用它作为对调用函数的特定对象的引用来编写fnCallback。

#3


arguments.caller or Function.caller but you might want to look at reorganizing your code to avoid using them. There really is another way to get what you want -- I guarantee it.

arguments.caller或Function.caller但您可能希望重新组织代码以避免使用它们。真的有另一种方法来获得你想要的东西 - 我保证。

Edit: AnthonyWJones pointed out that you're actually looking for the this operator.

编辑:AnthonyWJones指出你实际上正在寻找这个运营商。

The this keyword refers to the context object (a.k.a. current object). In general, in a method, this refers to the calling object.

this关键字引用上下文对象(a.k.a。当前对象)。通常,在方法中,这指的是调用对象。

#4


If you are looking for a way to find a non-function element that called a function, you might consider using the event. This is a pretty handy function I use a lot when passed an event object.

如果您正在寻找一种方法来查找调用函数的非函数元素,您可以考虑使用该事件。这是一个非常方便的函数,我在传递一个事件对象时经常使用它。

function getTargetElement(evt) {
  evt = (evt) ? evt : ((window.event) ? window.event : "");
  var elem;
  if (evt.target) {
      elem = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target;
  } else {
      elem = evt.srcElement;
  }
  return elem;
}

So, for example, if you call a function "find(event);" from a clicked element, find can be coded to use this method to get the event object:

所以,例如,如果你调用一个函数“find(event);”从一个被点击的元素中,find可以编码为使用此方法来获取事件对象:

function find(evt){
  var clickedObj = getTargetElement(evt);
  alert(clickedObj.tagName); //alerts "TD"
}