myDataSet[index - 1].data = Array.from(tmp);
在哪里,这是我收到的以下错误消息:
对象不支持匿名函数的属性或方法....(等)
我在这里做的是我有一个nath nath nature
Set()
,其中包含以下数据:
之后,我只是从此创建一个简单的数组对象。
我如何解决这个问题?
eDit 基于建议,我已将以下代码添加到我的应用中:Set
在以下文档模式中不支持:怪癖,Internet Explorer 6标准,Internet Explorer 7标准,Internet Explorer 8标准,Internet Explorer 9标准,Internet Explorer 10标准,Internet Explorer 11标准。 Windows 8.1不支持(兼容性参考)
即可在您的页面中添加代码(从developer.mozilla.org
复制JS代码)。它将模仿ES6的if (!Array.from) {
Array.from = (function () {
var toStr = Object.prototype.toString;
var isCallable = function (fn) {
return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
};
var toInteger = function (value) {
var number = Number(value);
if (isNaN(number)) { return 0; }
if (number === 0 || !isFinite(number)) { return number; }
return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
};
var maxSafeInteger = Math.pow(2, 53) - 1;
var toLength = function (value) {
var len = toInteger(value);
return Math.min(Math.max(len, 0), maxSafeInteger);
};
// The length property of the from method is 1.
return function from(arrayLike/*, mapFn, thisArg */) {
// 1. Let C be the this value.
var C = this;
// 2. Let items be ToObject(arrayLike).
var items = Object(arrayLike);
// 3. ReturnIfAbrupt(items).
if (arrayLike == null) {
throw new TypeError("Array.from requires an array-like object - not null or undefined");
}
// 4. If mapfn is undefined, then let mapping be false.
var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
var T;
if (typeof mapFn !== 'undefined') {
// 5. else
// 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
if (!isCallable(mapFn)) {
throw new TypeError('Array.from: when provided, the second argument must be a function');
}
// 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
if (arguments.length > 2) {
T = arguments[2];
}
}
// 10. Let lenValue be Get(items, "length").
// 11. Let len be ToLength(lenValue).
var len = toLength(items.length);
// 13. If IsConstructor(C) is true, then
// 13. a. Let A be the result of calling the [[Construct]] internal method of C with an argument list containing the single item len.
// 14. a. Else, Let A be ArrayCreate(len).
var A = isCallable(C) ? Object(new C(len)) : new Array(len);
// 16. Let k be 0.
var k = 0;
// 17. Repeat, while k < len… (also steps a - h)
var kValue;
while (k < len) {
kValue = items[k];
if (mapFn) {
A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
} else {
A[k] = kValue;
}
k += 1;
}
// 18. Let putStatus be Put(A, "length", len, true).
A.length = len;
// 20. Return A.
return A;
};
}());
}
方法。
第六版中的ECMA-262标准添加到ECMA-262标准中;作为 标准的其他实现可能不存在。 您可以通过在此处插入以下代码来解决此问题 脚本的开始,允许使用array.from 不能本地支持它的实现。 该算法是 正是ECMA-262,第6版中指定的一个,假设对象 typeError具有其原始值,该回调。 评估函数的原始值.prototype.call。在 此外,由于真正的迭代无法填充,所以 实施不支持在 ECMA-262的第6版。
Array.from
基本上需要从他的数组般的对象中创建简单的数组。我曾经品尝到最有效的2行Plain
Array.from
循环(我必须从html dom nodelist阵列般的对象制作数组,同样适用于JavaScriptif (!Array.from) {
Array.from = (function () {
var toStr = Object.prototype.toString;
var isCallable = function (fn) {
return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
};
var toInteger = function (value) {
var number = Number(value);
if (isNaN(number)) { return 0; }
if (number === 0 || !isFinite(number)) { return number; }
return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
};
var maxSafeInteger = Math.pow(2, 53) - 1;
var toLength = function (value) {
var len = toInteger(value);
return Math.min(Math.max(len, 0), maxSafeInteger);
};
// The length property of the from method is 1.
return function from(arrayLike/*, mapFn, thisArg */) {
// 1. Let C be the this value.
var C = this;
// 2. Let items be ToObject(arrayLike).
var items = Object(arrayLike);
// 3. ReturnIfAbrupt(items).
if (arrayLike == null) {
throw new TypeError("Array.from requires an array-like object - not null or undefined");
}
// 4. If mapfn is undefined, then let mapping be false.
var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
var T;
if (typeof mapFn !== 'undefined') {
// 5. else
// 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
if (!isCallable(mapFn)) {
throw new TypeError('Array.from: when provided, the second argument must be a function');
}
// 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
if (arguments.length > 2) {
T = arguments[2];
}
}
// 10. Let lenValue be Get(items, "length").
// 11. Let len be ToLength(lenValue).
var len = toLength(items.length);
// 13. If IsConstructor(C) is true, then
// 13. a. Let A be the result of calling the [[Construct]] internal method of C with an argument list containing the single item len.
// 14. a. Else, Let A be ArrayCreate(len).
var A = isCallable(C) ? Object(new C(len)) : new Array(len);
// 16. Let k be 0.
var k = 0;
// 17. Repeat, while k < len… (also steps a - h)
var kValue;
while (k < len) {
kValue = items[k];
if (mapFn) {
A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
} else {
A[k] = kValue;
}
k += 1;
}
// 18. Let putStatus be Put(A, "length", len, true).
A.length = len;
// 20. Return A.
return A;
};
}());
}
对象)。
对于OP的情况,它可能会以这种方式听起来:将其分开函数,您将获得3行通用可重复使用的解决方案在这里是单独函数的外观:
foreach回调的相关描述,以查看如何使用arguments
PS:这是
var temp_array = [],
length = tmp.length;
for (var i = 0; i < length; i++) {
temp_array.push(tmp[i]);
}
// Here you get the normal array "temp_array" containing all items
// from your `tmp` Set.
呼叫。
slice.call
用于类似数组的对象。这意味着您的代码将读取:
/**
* @param arr The array | array-like data structure.
* @param callback The function to process each element in the 'arr'.
* The callback signature and usage is assumed similar to the
* native JS 'forEach' callback argument usage.
*/
function customEach(arr, callback) {
'use strict';
var l = arr.length;
for (var i = 0; i < l; i++) {
callback(arr[i], i, arr);
}
};
虽然它不支持
ie,但您可以使用Mdn.
的<9 case.
polyfill。
在这里和MDN这里的第一个正确答案 问题的原始海报写道:
我在这里做什么,是我有一个名为customEach
但是我们在这里有3个答案已经超过4年了,并且Nobody已用一个对象对其进行了测试。
IT从MDN复制并粘贴到未经测试的情况下粘贴到接受的答案中。 我的多填充解决方案也比MDN的不正确和巨大的聚填料短得多。在以下解决方案中,您将在注释中找到有关函数及其参数的说明。