[英]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: 为了进行原型继承工作,您需要做两件事:
Use the new operator when creating the object, in order to have a this
with the correct prototype. 创建对象时使用new操作符,为了有一个this
用正确的原型。
new URLLoad(...)
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: 如果要避免暴露那些cache
和URLLoad
全局变量,我们可以使用模块模式将它们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.