Jquery 使用什么代替Object.keys()?
我需要在Jquery中找到一些既能在IE8中工作又能在真实浏览器中工作的东西。我是Jquery的新手,以下是我在现代浏览器中使用的代码:Jquery 使用什么代替Object.keys()?,jquery,Jquery,我需要在Jquery中找到一些既能在IE8中工作又能在真实浏览器中工作的东西。我是Jquery的新手,以下是我在现代浏览器中使用的代码: $('#FIELD_'+office_id).on('change',function(){ offices = $(this).val(); for(var i=0; i<=Object.keys(southland.address).length;i++){ if(offices == Object.keys(southlan
$('#FIELD_'+office_id).on('change',function(){
offices = $(this).val();
for(var i=0; i<=Object.keys(southland.address).length;i++){
if(offices == Object.keys(southland.address)[i]){
address = southland.address[offices]Object.keys(southland.address[offices])[0]];
}
}
$('#字段'+办公室id).on('change',function(){
offices=$(this.val();
对于(var i=0;i在旧浏览器中支持Object.keys,可以使用以下代码段:
if(!Object.keys){
Object.keys=(函数(){
var hasOwnProperty=Object.prototype.hasOwnProperty,
HasdontenUnbug=!({toString:null}).propertyIsEnumerable('toString'),
多特南=[
“toString”,
“Tolocalesting”,
“价值”,
“hasOwnProperty”,
“isPrototypeOf”,
“propertyIsEnumerable”,
“构造函数”
],
dontEnumsLength=dontEnums.length;
返回函数(obj){
如果(typeof obj!=='object'&&typeof obj!=='function'| | obj===null)抛出新的TypeError('object.keys在非对象上调用');
var结果=[];
用于(obj中的var prop){
if(hasOwnProperty.call(obj,prop))result.push(prop);
}
如果(hasDontEnumBug){
对于(变量i=0;i
或该多填料(也包括其他垫片):
IE8中的这段代码会发生什么情况?它会抛出任何错误,或者根本无法静默工作?这段代码的第5行靠近offices]对象有语法错误。keys
准确地说,它完全没有任何作用。只需polyfill对象。keys
这段代码需要放在特定的位置吗?非常感谢。它应该显示为above第一次使用Object.keys,但最好放在脚本的“polyfills”区域(你可以放其他垫片/插件/实用程序等),谢谢。如果是wordpress,你可以轻松地将其添加到当前主题文件夹的main.js中。
if (!Object.keys) {
Object.keys = (function () {
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function (obj) {
if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');
var result = [];
for (var prop in obj) {
if (hasOwnProperty.call(obj, prop)) result.push(prop);
}
if (hasDontEnumBug) {
for (var i=0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
}
}
return result;
};
})();
}