简体   繁体   English

Javascript:使用对象而不是数组按ID检索对象

[英]Javascript: retrieve objects by id, using an object, not an array

I want an array of objects to monitor (loading of) each span element (using XMLHTTPRequest to retrieve a URL on another page and place into the relevant span - I haven't included this functionality below as I'm only interested in the object array). 我想要一个对象数组来监视(跨接)每个span元素 (使用XMLHTTPRequest来检索另一个页面上的URL并将其放入相关的span中-我下面没有包含此功能,因为我只对object数组感兴趣)。

I liked the idea of the following (Doug Crockford?) snippet , but I just can't get it to work. 我喜欢以下代码片段(Doug Crockford?)的想法,但我无法使其正常工作。 I get: 我得到:

URLLoad.LoadingTxt000003.SetLoadTimeout is not a function

Any ideas? 有任何想法吗?

Here's my code: 这是我的代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>

<script language="javascript" type="text/javascript">

function URLLoad(id) {
    alert(id);

    return URLLoad[id] = {
        Tagid:id,
        LoadTimeout:0
    }
}

URLLoad.prototype.SetLoadTimeout = function(lt) {
    this.LoadTimeout = lt;
}

URLLoad("LoadingTxt000003");
URLLoad("LoadingTxt000005");

alert(URLLoad.LoadingTxt000003.DelayTimeout + URLLoad.LoadingTxt000005.DelayTimeout);

URLLoad['LoadingTxt000003'].SetLoadTimeout(20); //doesn't work
URLLoad.LoadingTxt000003.SetLoadTimeout(20);    //doesn't work

</script>

</head>

<body>

    <span id="LoadingTxt000003">...</span>
    <span id="LoadingTxt000005">...</span>

</body>
</html>

In order to have prototypical inheritance work you need to do two things: 为了进行原型继承工作,您需要做两件事:

  1. Use the new operator when creating the object, in order to have a this with the correct prototype. 创建对象时使用new操作符,为了有一个this用正确的原型。

     new URLLoad(...) 
  2. Not return a value from the constructor. 从构造函数返回值。 The value you are returning doesn't have the correct prototype but has precedence anyway. 您返回的值没有正确的原型,但是仍然具有优先级。

     function URLLoad(){ this.tagId = ...; this.timeOut = ...; } 

Of course you might now complain that I am not doing the cacheing. 当然,您现在可能会抱怨我没有进行缓存。 Well, I think it is best to separate the construction from the cacheing. 好吧,我认为最好将构造与缓存分开。

var cache = {};
function urlLoad(key){
    if(!cache[key]){ cache[key] = new URLLoad(key); }
    return cache[key];
}

If you want to avoid having those exposed cache and URLLoad globals, we can make them private with the module pattern: 如果要避免暴露那些cacheURLLoad全局变量,我们可以使用模块模式将它们URLLoad私有:

var urlLoad = (function(){

    function URLLoad(){...};
    URLLoad.prototype = {...};

    var cache = {};
    function urlLoad(){...};

    return urlLoad;
}()); 

URLLoad is a constructor. URLLoad是一个构造函数。 Try prefixing it using the new operator: 尝试使用new运算符添加前缀:

new URLLoad("LoadingTxt000003");

I think your solution is overly complicated. 我认为您的解决方案过于复杂。 If you want to cache information about your views, just use an object literal and do 如果要缓存有关视图的信息,只需使用对象文字并执行

var viewCache = {};

Then to add views in, just do 然后在其中添加视图

viewCache['LoadingTxt000003'] = 20;

Note that this uses the id of the view element as the key and the timeout as the val, in the cache. 请注意,这在缓存中使用view元素的id作为键,将超时用作val。 Also note that even though [id] looks like an array index, when used on a object literal its just a way to set the key/value pair. 还要注意,即使[id]看起来像数组索引,当在对象文字上使用它时,它只是设置键/值对的一种方法。

You can expand it by doing something like 您可以通过执行以下操作来扩展它

viewCache['LoadingTxt000003'] = {'timeout': 20};

Now your cache is an object literal, and its values are object literals as well, which is useful if you are going to store more than one piece of information in your cache. 现在,您的缓存是一个对象文字,它的值也就是对象文字,如果您要在缓存中存储多个信息,这很有用。

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

相关问题 通过对象的id在javascript数组中查找和移动对象 - Find and move an object in javascript array by id of an objects JavaScript将具有相同ID的对象转换为对象数组 - JavaScript converting object with same id to array of objects 在 JavaScript 个对象的数组中按 id 查找 object - Find object by id in an array of JavaScript objects 如何使用 javascript 从对象数组中检索选择性属性并存储为 object? - How to retrieve selective properties from the array of objects and store as an object using javascript? 如何从对象数组中检索随机对象? 使用Javascript - How do I retrieve a random object from an array of objects ? Javascript JavaScript:如何在不使用for..in循环的情况下,从给定ID的对象数组中获取对象? - JavaScript: How to Fetch a object from array of objects, given an id, without using for..in loop? 如何使用 javascript 从与 id 匹配的对象的递归数组中找到 object 并做出反应? - How to find an object from recursive array of objects that is matching with the id using javascript and react? 如何使用JavaScript在对象数组中添加对象 - How to add object in an array of objects using Javascript 使用javascript将对象转换为对象数组 - Convert object to array of objects using javascript JavaScript:使用对象更新 object 的数组 - JavaScript: update array of object using objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM