我一直在阅读并做一些测试,发现了这个命令.当数组的键是字符串时,Array()javascript的"长度"不起作用.我发现要运行此命令,我使用整个键.
但是,我想知道你们中有谁能告诉我为什么这条规则?语言的限制,还是逻辑的规范?或者,我的错误......?
谢谢
Obs.:我的数组的关键是一个字符串(组件名称),但是值和对象数组.
Declarating:
objElementos = new Array();
Run Code Online (Sandbox Code Playgroud)
宾语:
objElemento = function(Id){
this.Id = $('#' + Id).attr('id');
this.Name = $('#' + Id).attr('name');
this.CssLeft = $('#' + Id).css('left');
this.CssBottom = $('#' + Id).css('bottom');
this.Parent = $('#' + Id).parent().get(0);
this.Childs = new Array();
this.addChild = function(IdObjChild) {
try {
if ($('#' + IdObjChild).attr('id') != '')
this.Childs[$('#' + IdObjChild).attr('id')] = new objElemento(IdObjChild);
} catch(err){ $('#divErrors').prepend('<p>' + err + '</p>'); }
}
this.getChildCount = function(){
try {
$('#divErrors').prepend('<p>' + this.Childs.length + '</p>');
return this.Childs.length;
} catch(err){ $('#divErrors').prepend('<p>' + err + '</p>'); }
}
this.updateCssLeft = function(CssPosition){
try {
$('#divErrors').prepend('<p>updateCssLeft:' + this.Id + ' / ' + this.CssLeft + '</p>');
$('#' + this.Id).css('left',CssPosition);
this.CssLeft = $('#' + this.Id).css('left');
$('#divErrors').prepend('<p>updateCssLeft:' + this.Id + ' / ' + this.CssLeft + '</p>');
} catch(err){ $('#divErrors').prepend('<p>' + err + '</p>'); }
}
this.updateCssBottom = function(CssPosition){
try {
$('#divErrors').prepend('<p>updateCssBottom:' + this.Id + ' / ' + this.CssBottom + '</p>');
$('#' + this.Id).css('bottom',CssPosition);
this.CssBottom = $('#' + this.Id).css('bottom');
$('#divErrors').prepend('<p>updateCssBottom:' + this.Id + ' / ' + this.CssBottom + '</p>');
} catch(err){ $('#divErrors').prepend('<p>' + err + '</p>'); }
}
}
Run Code Online (Sandbox Code Playgroud)
使用:
objElementos['snaptarget'] = new objElemento('snaptarget');
Run Code Online (Sandbox Code Playgroud)
在数组上使用字符串"indexers"时,您实际上将其视为对象而不是数组,并将额外字段添加到该对象上.如果您没有使用整数索引,那么使用Array类型并没有多大意义,而使用Object类型更有意义.您可以按如下方式计算字段:
var c=0;
for(var fieldName in obj)
{
c++;
}
Run Code Online (Sandbox Code Playgroud)