[英]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>
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 中获取该方法。您实际上只有这些选项和您可以选择看起来最好或最不令人反感的一个:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.