javascript关联数组 - 属性 [英] javascript associative arrays - properties

查看:97
本文介绍了javascript关联数组 - 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经从我们公司的开发人员手中接过了,我遇到了一个jS文件,



它声明

Hi i've took over from a developer in our company and i've come across a jS file,

it declares

var requiredTemplates = {};





在页面顶部,然后稍后确实





at the top of the page, and then later on does

requiredTemplates.toolBarTemplate = new Application.Template() *custom class
requiredTemplates.uploadTemplate = new Application.Template()





等等,



我无法理解的是没有对requiredTemplates的引用,它从哪里获取这些属性,或者它们是什么只是在飞行中制作,可以这样做,因为我一直认为关联数组需要键值对



喜欢



and so on,

what i can't understand is there is no reference to requiredTemplates, and where does it get these properties from, or they simply being made on the fly, can this be done, as i always thought associative arrays needed key value pair

like

 var requiredTemplates = {

     uploadTemplate: value;
}





任何帮助清理它都会很棒: - )



any help in clearing that up would be great :-)

推荐答案

它们将在运行中创建。另一种表示法是 obj [key] = value



它们实际上并不是真正的关联数组。它是一个具有属性的对象,因此您将无法以与使用数组相同的方式使用它(例如 for(var i = 0,len = obj.length; i< len ; ++ i){}



如果您要循环使用它,您需要执行以下操作

They will be created on the fly. An alternative notation would be obj[key] = value.

They are not really associative arrays actually. It's an object with properties, so you won't be able to use it in the same way you would use an array (e.g. for (var i = 0, len = obj.length; i < len; ++i) {})

If you were to loop through it, you need to do as following
var o = {};
o.key1 = 'value1';
o.key2 = 'value2';

for (var key in o) {
    if (!o.hasOwnProperty(key)) continue;
    
    var value = o[key];
}


这篇关于javascript关联数组 - 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆