对象不支持属性或方法“条目” [英] Object doesn't support property or method 'entries'

查看:758
本文介绍了对象不支持属性或方法“条目”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

解决方案

基本上,polyfill是一种可以手动定义特定平台/浏览器本身不支持的功能的方法。



在您的情况下,此处给出了函数 Object.entries 的基本定义:
< a href =https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries#Polyfill =noreferrer> https://developer.mozilla.org/ en-US / docs / Web / JavaScript / Reference / Global_Objects / Object / entries#Polyfill



他们提供了这个简单的,随时可以部署的定义:

  if(!Object.entries)
Object.entries = function(obj){
var ownProps = Object .keys(obj),
i = ownProps.length,
resArray = new Array(i); //预分配数组
而(i--)
resArray [i] = [ownProps [i],obj [ownProps [i]]];

返回resArray;
};

查看上面的代码,首先检查的是 Object。条目存在。如果确实如此,不用担心,但如果它不存在,那么就创建它...
只要在你的代码中实际调用它之前定义了这个函数,你应该没问题。



使用 angular-cli 之类的东西,他们提供了polyfill。 ts文件(在你的应用程序运行之前执行)你可以在这里放置这样的代码或导入包含你需要的定义的文件。






10/30/2018更新:



@apsillers正确指出上述答案不适用于 FormData.entries(),但改为 Object.entries()



的解决方案FormData.entries()(这对我有用):
https://stackoverflow.com/a/49556416/3806701



基本上,导入此多面填充:

 < script src =https://unpkg.com/formda TA-填充工具>< /脚本> 

然后你可以迭代 FormData ,如下所示:

  var formDataEntries =(< any> formData).entries(),formDataEntry = formDataEntries.next(),pair; 
while(!formDataEntry.done){
pair = formDataEntry.value;
console.log(pair [0] +','+ pair [1]);
formDataEntry = formDataEntries.next();
}


I am working with the FormData object, while my code works well on Chrome, Microsoft Edge spits out the following error message Object doesn't support property or method 'entries' – which corresponds to the following code:

for(let pair of formData.entries()) {
  ...
}

I've tried replacing .entries() with .getAll(), however Microsoft Edge doesn't recognize either of both methods.

Is there a way to get this functionality (iterating over FormData files) out of Microsoft Edge?

FormData Microsoft Edge Console Dump

解决方案

Essentially, a polyfill is a way you can manually define a function that isn't natively supported on a specific platform/browser.

In your case, there is a basic definition of the function Object.entries given here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries#Polyfill

They provide this simple, ready to deploy definition:

if (!Object.entries)
  Object.entries = function( obj ){
    var ownProps = Object.keys( obj ),
        i = ownProps.length,
        resArray = new Array(i); // preallocate the Array
    while (i--)
      resArray[i] = [ownProps[i], obj[ownProps[i]]];

    return resArray;
  };

Looking at the code above, the first thing it checks is if Object.entries exists. If it does, no worries, but if it doesn't exist, then it creates it... As long as this function gets defined before you actually call it in your code, you should be fine.

Using something like angular-cli, they provide a polyfills.ts file (which gets executed before your app is run) where you can place code like this or import files containing definitions you'll need.


10/30/2018 UPDATE:

@apsillers correctly pointed out the answer above does not apply to FormData.entries(), but to Object.entries() instead.

Solution for FormData.entries() (this worked for me): https://stackoverflow.com/a/49556416/3806701

Basically, import this poly-fill:

<script src="https://unpkg.com/formdata-polyfill"></script>

Then you can iterate FormData as follows:

var formDataEntries = (<any>formData).entries(), formDataEntry = formDataEntries.next(), pair;
while (!formDataEntry.done) {
    pair = formDataEntry.value;
    console.log(pair[0] + ', ' + pair[1]);
    formDataEntry = formDataEntries.next();
}

这篇关于对象不支持属性或方法“条目”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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