JavaScript 对象错误
这是我的情况:
var r = {"id":"test","files":[],"code":""};
var Page = Class.create({
initialize: function(json) {
this.id = json.id;
this.json = json;
},
start: function() {
this.json.files.each(function(x) {
var cssResource = this.pageResource(x, 'css');
this.attachCssResource(cssResource);
}.bind(this));
},
//etc....
});
var P = new Page(r);
P.start();
嗨,this.json.files.each 不是一个函数..为什么?
我使用原型 1.7 库。
如果我使用这个
var P = new Page({"id":"test","files":[],"code":""});
我没有错误??....
This is my situation:
var r = {"id":"test","files":[],"code":""};
var Page = Class.create({
initialize: function(json) {
this.id = json.id;
this.json = json;
},
start: function() {
this.json.files.each(function(x) {
var cssResource = this.pageResource(x, 'css');
this.attachCssResource(cssResource);
}.bind(this));
},
//etc....
});
var P = new Page(r);
P.start();
Hi have this.json.files.each is not a function.. why?
I use prototype 1.7 library.
if i use this
var P = new Page({"id":"test","files":[],"code":""});
i have NO error??....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要扩展数组上的 PrototypeJS 函数才能使每个函数正常工作:
http: //www.prototypejs.org/api/utility/dollar-a
http:// www.prototypejs.org/api/element/extend
You may need to extend the PrototypeJS functions on your array to get the each function to work:
http://www.prototypejs.org/api/utility/dollar-a
http://www.prototypejs.org/api/element/extend
如果您仅针对 ECMAScript 5 平台,您将需要使用 Array.forEach。
15.4.4.18 Array.prototype.forEach (callbackfn [ , thisArg ] )
callbackfn 应该是一个接受三个参数的函数。 forEach 对数组中存在的每个元素按升序调用一次callbackfn。 callbackfn 仅针对实际存在的数组元素调用;数组中缺少元素时不会调用它。
如果提供了thisArg参数,它将用作每次调用callbackfn的this值。如果未提供,则使用未定义。
callbackfn 使用三个参数调用:元素的值、元素的索引和正在遍历的对象。
forEach 不会直接改变调用它的对象,但该对象可能会通过调用callbackfn来改变。
forEach 处理的元素范围是在第一次调用callbackfn之前设置的。调用 forEach 开始后附加到数组的元素将不会被 callbackfn 访问。如果数组的现有元素发生更改,则传递给回调的值将是 forEach 访问它们时的值;在调用 forEach 开始之后且在被访问之前被删除的元素不会被访问。
If you are targeting only ECMAScript 5 platforms you will want to use
Array.forEach
.15.4.4.18 Array.prototype.forEach ( callbackfn [ , thisArg ] )
callbackfn should be a function that accepts three arguments. forEach calls callbackfn once for each element present in the array, in ascending order. callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array.
If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfn. If it is not provided, undefined is used instead.
callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.
forEach does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.
The range of elements processed by forEach is set before the first call to callbackfn. Elements which are appended to the array after the call to forEach begins will not be visited by callbackfn. If existing elements of the array are changed, their value as passed to callback will be the value at the time forEach visits them; elements that are deleted after the call to forEach begins and before being visited are not visited.
也许您可以使用以下方法将其强制转换为数组:
有时我发现使用散列而不是数组更容易 -
$H
而不是$A
。Perhaps you can forcibly cast it to an array with:
Sometimes I find it easier to work with a hash instead of an array --
$H
instead of$A
.