简体   繁体   English

OO JavaScript:访问同一对象的另一种方法?

[英]OO JavaScript: Accessing another method of the same object?

I have a JavaScript object called ShippingUI: 我有一个名为ShippingUI的JavaScript对象:

function ShippingUI(){
   ...
}

It has several methods: 它有几种方法:

ShippingUI.prototype.UpdateItemQTYs = function(ItemJQOBJ, NewQTY)
{
   ...
}

ShippingUI.prototype.EH_SortableRecieve = function(event, ui)
{
    ...
}

The "EH_SortableRecieve()" function is a drop event handler. “ EH_SortableRecieve()”函数是放置事件处理程序。 When it runs, it needs to call "UpdateItemQTYs()", a sister function in the same object. 运行时,它需要调用“ UpdateItemQTYs()”,即同一对象中的姊妹函数。 I'm trying to use: 我正在尝试使用:

ShippingUI.prototype.EH_SortableRecieve = function(event, ui)
{
    this.UpdateItemQTYs('ABCD',123);
}

But keep getting the error: 但是,请继续获取错误:

 "this.UpdateItemQTYs is not a function"

I'm guessing that "this" is pointing to something else... so how do I get the 'real' "this"? 我猜测“ this”指向其他事物……那么我如何获得“真实的”“ this”?

Event Binding method: 事件绑定方法:

// Takes a Jquery Object and makes it sortable with our custom options
   this.MakeSortable = function(JQOBJ)
   {
     JQOBJ.sortable({
       connectWith: '.connectedSortable',
       items: '.ItemLineWrapper',
       dropOnEmpty: true,
       axis: 'y',
       receive: this.EH_SortableRecieve       
       });
   }

There is something missing in your examples which is how EH_SortableRecieve is called. 您的示例中缺少一些东西,即如何调用EH_SortableRecieve。 But based on what you say it should be used as I'm thinking it is used something like this: 但是根据您所说的,应该使用它,因为我认为它的用法如下:

htmlelement.onmouseup = shippingObject.EH_SortableRecieve;

In which case you should be aware of Javascript's binding of this in methods. 在这种情况下,你应该知道JavaScript的结合的this的方法。 Specifacally, in event handlers this is bound to the window object instead of the object the method belongs to. 具体来说,在事件处理程序中, this方法绑定到window对象,而不是方法所属的对象。 This is a general feature of javascript: methods can be re-bound at runtime. 这是javascript的一般功能:方法可以在运行时重新绑定。 In other words, objects can steal other object's methods. 换句话说,对象可以窃取其他对象的方法。 For example, I can have my object slebetmans_object steal your method and re-bind its this with the following: 例如,我可以有我的对象slebetmans_object窃取你的方法,并重新绑定了this有以下:

shippingObject.EH_SortableRecieve.apply(slebetmans_object,parameters);

There are several strategies to get around this. 有几种策略可以解决此问题。 You can use a closure to capture your object: 您可以使用闭包捕获对象:

htmlelement.onmouseup = function(){ shippingObject.EH_SortableRecieve() };

You can use a closure in the object's constructor to capture the correct reference to your object: 您可以在对象的构造函数中使用闭包来捕获对对象的正确引用:

function ShippingUI () {
    var self = this; // since self is resolved at the time the object is created
                     // it always reference to the "correct" object

    this.EH_SortableRecieve = function(event, ui)
    {
        self.UpdateItemQTYs('ABCD',123);
    }
}

There are probably other ways to do this but these are the two most common that I personally use. 可能还有其他方法可以执行此操作,但是这是我个人最常用的两种方法。

The problem is that when you register the function as an event handler, its relationship to your object is lost. 问题在于,当您将该函数注册为事件处理程序时,该函数与对象的关系将会丢失。

How are you registering the event handler? 您如何注册事件处理程序? If it's with jQuery, you can use the new "proxy" API: 如果是jQuery,则可以使用新的“代理” API:

$('#someButton').click($.proxy(myShippingUI, 'EH_SortableReceive'));

That'll make sure the object (I used "myShippingUI" as a sample instance of your class) is acting as the "this" pointer. 这将确保对象(我使用“ myShippingUI”作为类的示例实例)充当了“ this”指针。 There are other ways to do this to, but that's a simple one. 还有其他方法可以做到这一点,但这很简单。

The "$.proxy" thing is new with 1.4. “ $ .proxy”是1.4中的新增功能。

function ShippingUI(){
   ...
}

ShippingUI.prototype = {
UpdateItemQTYs : function(ItemJQOBJ, NewQTY)
{
   ...
},
that = this,
EH_SortableRecieve = function(event, ui)
{

that.UpdateItemQTYs('ABCD',123);
    ...
}
};

The above should work too... All you need is the reference to ShippingUI object you are currently using in this. 上面的方法也应该起作用...您所需要的只是对此中当前使用的ShippingUI对象的引用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM