简体   繁体   English

先前对象无法使用的处理程序。 只有最后一件作品

[英]Handler for previous objects not working. Only last one works

I want to return a new handler every time for a new object that I create. 我想每次为我创建的新对象返回一个新的处理程序。 This handler takes one object argument. 该处理程序采用一个对象参数。

function abc () {
    var handle = function(position) {
        // some code goes here
    };
    var obj1 = new SomeOBJ ();
    obj1.handler = handle;
    return obj1;
}

When I create one object it works fine, but when I create another object the previous handler doesnot work anymore, only the latest object's handler works. 当我创建一个对象时,它工作正常,但是当我创建另一个对象时,先前的处理程序不再起作用,只有最新对象的处理程序可以工作。

For me it looks like the latest only one handle is being created and is attached to the latest object every time. 对我来说,似乎只有最新的一个句柄正在创建,并且每次都附加到最新的对象。

What can be the reason for this? 这可能是什么原因?

Thank you. 谢谢。

I can't figure out what you exactly are trying to do and what you are doing. 我无法弄清楚您到底想做什么和正在做什么。 If you are trying to have separate methods on each instance of a class you can assign the method on initialize or after the init function: 如果要在类的每个实例上尝试使用单独的方法,则可以在初始化时或在init函数之后分配该方法:

function Klass() {
    // Assign a specific method for each instance of this class:
    this.handler = function() {};
}
// This method is the same around all instances of this class:
Klass.prototype.otherMethod = function() {};
// Create three instance of the class to compare:
var klass = new Klass();
var klass2 = new Klass();
var klass3 = new Klass();

// See the handler method is different on each instance:
console.log(klass.handler === klass2.handler); // false;
// And the methods set through the prototype is shared:
console.log(klass.otherMethod === klass2.otherMethod); // true;

// Give klass3, klass2's handler:
klass3.handler = klass2.handler;
// These are shared as well because of what we just did.
console.log(klass2.handler === klass3.handler); // true;

klass3.otherMethod = function() {};
// Because klass3 have got another method assigned to 'otherMethod' this will return false
console.log(klass2.otherMethod === klass3.otherMethod); // false;

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

相关问题 jQuery滑块不起作用。 下一个和上一个按钮分别在最后一个图像和第一个图像时应该消失 - JQuery Slider Not Working. Next and Previous Buttons should disappear when at the last and first image respectively 输入的多个参考,仅适用于最后一个 - multiple refs for inputs, works only last one JavaScript-对象原型中的Getter / Setter无法正常工作。 Getter返回最后一个元素 - JavaScript - Getter/Setter in Objects's Prototype is not working. Getter returns last element 在循环中构建多个 SlimSelect 对象 - 只有最后一个对象有效 - building multiple SlimSelect objects in a loop - only last object works 输入javascript验证仅在最后一个有效 - on input javascript validation is working only in last one jquery mouseover无法正常工作。 只在jfiddle工作 - jquery mouseover not working. working in jfiddle only node.js中的群集无法正常工作。 只有一名工人总是在回应 - clustering in node.js is not working. Only one worker is always responding AngularJS分成多个文件 - 只有最后一个文件有效 - AngularJS split into multiple files - only the last one works 仅最后输入有效 - Only last Input works 无法将javascript代码移至设计视图。 仅代码隐藏的Attributes.Add(“ onclick”有效。 - Moving the javascript code to design view is not working. Only code-behind Attributes.Add(“onclick” works. Puzzled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM