简体   繁体   English

如何从 object 外部(通过事件)引用 javascript 中的 object 方法?

[英]How do I reference an object method in javascript from outside the object (through event)?

I am creating a fairly complex object and want to be able to manipulate the object from the outside.我正在创建一个相当复杂的 object 并希望能够从外部操作 object。 I want to reference an privileged method from a click event我想从点击事件中引用特权方法

function myObject(){
   //How do I reference this function?
   this.doSomething = function (action){
        var doing = action;
   }

}

I understand that I could reference the method if I create the object inside a variable like:我知道如果我在如下变量中创建 object 可以参考该方法:

var obj = new myObject();
obj.doSomething('hit keyboard');

But the links I am creating to trigger this event are being created by the object, but placed outside the object, so they will not know the container variable to reference it.但是我为触发此事件而创建的链接是由 object 创建的,但放置在 object 之外,因此他们不会知道要引用它的容器变量。

ie IE

<a href="#" onclick="doSomething()">Do Something</a>

doesn't work because of course the doSomething method is part of the object, not a global function.不起作用,因为 doSomething 方法当然是 object 的一部分,而不是全局 function。 I could rewrite the code to make it global, but would rather avoid doing that.我可以重写代码以使其全局化,但宁愿避免这样做。

If this question doesn't make sense I would be happy to make it clearer.如果这个问题没有意义,我很乐意让它更清楚。 Thanks in advance.提前致谢。

Although it will sit in the global scope, and generally not recommended, you could do this (if you just need to get it working):虽然它将位于全局 scope 中,并且通常不推荐,但您可以这样做(如果您只需要让它工作):

function myObject(){
   //How do I reference this function?
   window.doSomething = function (action){ // USE window instead of this !!
        var doing = action;
   }

}

By using window instead of this , it can be called in your handler.通过使用window而不是this ,可以在您的处理程序中调用它。

IF the object instance is already created, then you can say:如果 object 实例已经创建,那么你可以说:

<a href="#" onclick="obj.doSomething()">Do Something</a>

Otherwise,否则,

<a href="#" onclick="function () { return new myObject().doSomething(); }">Do Something</a>

You need the object to be in the global scope.您需要 object 位于全局 scope 中。 Try this:尝试这个:

window.myObject = {
    doSomething: function(action) {
        alert("Done!");
    }
};

and then you make a click event like然后你做一个点击事件,比如

<a href="#" onclick="myObject.doSomething()">Do Something</a>

Example例子

The problem is that there's a disconnect between the instance of an object and the element on the page whose event should trigger the object's function.问题是 object 的实例与页面上其事件应触发对象的 function 的元素之间存在断开连接。 There are various ways to handle that without exposing anything globally.有多种方法可以处理该问题,而无需在全球范围内公开任何内容。 One way, for example, would be to tie the two via an object's property:例如,一种方法是通过对象的属性将两者联系起来:

Give the element an ID:给元素一个 ID:

<a id="myLink">Do Something</a>

Add element property to object:将元素属性添加到 object:

function myObject(element){
   this.element = element;

   if (this.element) {
      this.element.onclick = this.doSomething;
   }

   this.doSomething = function () {
         // do stuffs.
   }
}

Instantiate object:实例化 object:

var obj = new myObject(document.getElementById('myLink'));

Or simplify the object definition a bit more:或者进一步简化 object 定义:

function myObject(element){
   this.element = element;

   if (this.element) {
      this.element.onclick = function () {
         // do stuffs.
      }
   }
}

To define the problem, you're in a particular object's click handler and you want to call a method on another object in your code that you can't get to directly from the object you're in. You really only have these options and you can just pick the one that seems best or seems the least objectionable:要定义问题,您在特定对象的单击处理程序中,并且您想在您的代码中调用另一个 object 上的方法,您无法直接从您所在的 object 中获取该方法。您实际上只有这些选项和您可以选择看起来最好或最不令人反感的一个:

  1. You can make the object that you want to call a method on be in the global scope.您可以使要调用方法的 object 位于全局 scope 中。
  2. You can make the object that you want to call a method on be findable in the global scope (either in a parent hierarchy or with some sort of global find).您可以使 object 可以在全局 scope 中找到(在父层次结构中或通过某种全局查找)。 It can be done this way without necessarily adding any more top level objects to the global scope.可以通过这种方式完成,而不必向全局 scope 添加任何更多顶级对象。
  3. You can set the object that you want to call a method as instance data on all objects that will have the click handler so that you can just reference the global object from the click handler object's instance data.您可以将要调用方法的 object 设置为将具有单击处理程序的所有对象的实例数据,以便您可以从单击处理程序对象的实例数据中引用全局 object。
  4. You can declare the click handler to have a parameter that contains your object by using function closures and avoid all global data.您可以使用 function 闭包声明单击处理程序以具有包含 object 的参数并避免所有全局数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从javascript中返回的对象引用私有方法? - How can i reference a private method from returned object in javascript? 您如何在使用JavaScript创建的自定义对象之外引用自定义对象? - How do you reference a custom object outside of the function it was created in with JavaScript? 如何从同一对象的方法引用属性? - How do I reference property from method of the same object? 如何从类引用实例化javascript对象? - How do I instantiate a javascript object from class reference? Javascript-事件方法中的引用对象 - Javascript - Reference object inside event method 如何通过属性的 onclick 方法引用 javascript object? - How can I reference a javascript object through a property's onclick method? 如何通过通过Javascript创建的html对象(按钮)使用jQuery执行事件? - How do I execute an event using jQuery through an html object(button) created through Javascript? JavaScript:如何在对象键中引用对象属性? - JavaScript: How do I reference an object property in an object key? 在Angular 6中使用ngrx从外部组件发出事件时,如何更新状态对象? - How do I update state object when an event is emitted from an outside component using ngrx with Angular 6? 对JavaScript中的Event对象的引用 - Reference to Event object in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM