简体   繁体   English

使用构造函数的参数值查找对象的名称

[英]Finding the object's name using the constructor's argument's values

So using the following part of my code, I've been trying to find the object's name ("object's name" refers to foo, bar, baz, and jar below. Sorry if that's the improper term, couldn't find any examples like this one on the web that termed them) using the values of the constructed function. 因此,使用我的代码的以下部分,我一直在尝试查找对象的名称(“对象的名称”是指下面的foo,bar,baz和jar。很抱歉,如果这是不合适的术语,找不到类似的示例在网路上用构造函数的值来称呼它们)。 To give an example: 举个例子:

function myFunction(){
  foo = new component(50, 50, "first", "red");
  bar = new component(50, 100, "sec", "red");
  baz = new component(0, 50, "third", "blue");
  jar = new component(0, 100, "fourth", "blue");
}
function component(x, y, id, color){
this.x = x;
this.y = y;
this.id = id;
this.color = color;
}

So if I have the value of both x and y, 50 and 100 respectively, what method would I use to make it so that the program will recognize that the constructor with these values is bar? 因此,如果我分别拥有x和y的值,分别为50和100,那么我将使用哪种方法制作该值,以便程序识别具有这些值的构造函数为bar? If doing this using two values is impossible, using only one value is totally fine since I can always just combine the two into one. 如果不可能使用两个值来执行此操作,则仅使用一个值是完全可以的,因为我总是可以将两个值组合为一个。 So far the best thing I've been able to come up with that somehwhat worked was that "foo instanceof component" is true, so maybe there's some method that I've yet to find that's basically the opposite of instance of? 到目前为止,我能想到的最好的事情是“ foo instanceof component”是正确的,所以也许我还没有找到某种方法与它的instance相反呢? Thank you in advance. 先感谢您。

In your code foo , bar , baz and jar are variables, not constructors. 在您的代码foobarbazjar是变量,而不是构造函数。 These variables are pointing to objects that were created using the component function as the constructor. 这些变量指向使用component函数作为构造函数创建的对象。

It sounds like what you're trying to do is to find the object that was created using the specific values for x and y. 听起来您想要做的就是找到使用x和y的特定值创建的对象。 One way to do that would be to have a lookup table where the keys are the combination of x and y, and the values are the objects: 一种方法是拥有一个查找表,其中的键是x和y的组合,而值是对象:

var objects = {};
function component(x, y, id, color) {
    this.x = x;
    this.y = y;
    this.id = id;
    this.color = color;

    // Insert the current object into the lookup table
    objects[x + '_' + y] = this;
}

// Retreive an object from the lookup table
function findObject(x, y) {
    return objects[x + '_' + y];
}

One problem with this approach is that if you create two objects with the same x and y values, only the last one will be stored in the lookup table. 这种方法的一个问题是,如果创建两个具有相同x和y值的对象,则仅最后一个存储在查找表中。 You could solve that by storing an array of objects for each key instead. 您可以通过为每个键存储一个对象数组来解决此问题。

Using Nico's code along with Jaromanda X's self-containing code, I was able to update objects to allow the user to find the key (or is it variable, I'm still a bit unsure, whatever "foo" is) based on the new x and y values. 使用Nico的代码以及Jaromanda X的自包含代码,我能够基于新对象更新对象以允许用户找到键(或者它是否是变量,我仍然不确定,无论“ foo”是什么) x和y值。 Using 使用

function myFunction(){
  foo = new component(50, 50, "first", "red");
  bar = new component(50, 100, "sec", "red");
  baz = new component(0, 50, "third", "blue");
  jar = new component(0, 100, "fourth", "blue");
}
var component=(function(){
  var component=function(x, y, id, color){
    this.x = x;
    this.y = y;
    this.id = id;
    this.color = color;
    objects[x+'_'+y] = this;
    this.newPos = function(x, y){
      this.x = x;
      this.y = y;
      //objects[this.x+"_"+this.y] = this;
    };
  };
  component.get = function(x, y){
    return objects[x+'_'+y];
  };
  return component
})();

'this.newPos' will change this' x and y, depending what its parameters are, but this alone will force the user to continue to use the key/variable's original x and y values, as RobG pointed out. 正如RobG指出的那样,“ this.newPos”将更改其“ x”和“ y”,具体取决于其参数是什么,但这仅会迫使用户继续使用键/变量的原始x和y值。 To fix this, just remove the comment in newPos. 要解决此问题,只需删除newPos中的注释。 The code will make 'objects' x and y values to what 'this.x' and 'this.y' are in newPos, which will be the new values. 该代码将使“对象”的x和y值与newPos中的“ this.x”和“ this.y”相同,这将是新值。

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

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