将对象分配给数组,然后根据位置从数组调用它
Assigning objects to an array, then call it from the array based on position
我一直在研究这段代码,但我对为什么它不起作用感到困惑。我已经创建了三个"Geocache"对象,并将它们分配给一个名为"缓存"的数组。为了进行测试,我将变量"value"分配给缓存的索引1中的任何内容。然后,我将变量"挑逗"定义为值的"长度"属性。
function Geocache (lat, lon, disc){
this.laty=lat;
this.lony=lon;
this.disc=disc;
}
var loc1=new Geocache(43.77416104496804, -79.50804830784301, "lifesci building");
var loc2=new Geocache(43.77381242916627, -79.50533927673797, "lassonde building");
var loc3=new Geocache(43.77305321438563, -79.50353146786193, "vari hall");
var caches=[loc1,loc2,loc3];
var value = caches[1];
var tease=value.lony;
这应该返回值"-79.505…",但我在预览中得到的是"未定义"。有没有人能帮我弄清楚这段代码有什么问题,因为我似乎不能弄清楚。
这是你的代码,javascript在代码片段的javascript部分。
和一个使用自调用函数来阻止全局泄漏的修复。
function Geocache (lat, lon, disc){
this.laty=lat;
this.lony=lon;
this.disc=disc;
}
(function () {
var loc1=new Geocache(43.77416104496804, -79.50804830784301, "lifesci building");
var loc2=new Geocache(43.77381242916627, -79.50533927673797, "lassonde building");
var loc3=new Geocache(43.77305321438563, -79.50353146786193, "vari hall");
var caches=[loc1,loc2,loc3];
var value = caches[1];
var tease=value.lony;
console.log(tease);
})();
相关文章:
- 如何从某个位置顺时针和逆时针循环数组
- 将javascript数组中的项移动到特定位置的有效方法
- 生成ACF标记位置的数组(元素列表后缺少])
- 基于javascript中位置的数组差异
- Javascript删除数组中某个位置之后的项
- 在数组中迭代时的JS循环位置
- 在两个位置显示数组中的随机单词
- javascript递归,按数组位置查找子级
- 检查数组位置是否真的未定义
- 检查值存在于数组位置
- 在数组位置获取并设置cookie值
- 如何访问成员模板中的数组位置
- 递增数组位置
- 返回最低数字的数组位置(非负数)
- 我如何使一个按钮改变Javascript数组位置
- 我如何保存字符串的每个字母在不同的数组位置?并合并回单个字符串
- 下划线-比较两个对象数组(位置)
- for循环中的Ajax不会返回数组位置的正确值,尽管闭包绑定了当前值位置的范围
- 根据对象在Firebase中的数组位置检索对象
- 当使用x作为索引号时.要在函数中显示数组位置,数组返回undefined