IE8原型的方法错误

时间:2015-03-16 08:30:03

标签: javascript internet-explorer

这是我的问题,我已经定义了功能服务

function Service(){}

Service.prototype = {
getGroup: function(id){
        var group = null;
        if(this.groups.length > 0){
            $.each(this.groups, function(){
                if(this.id == id){
                    group = this;
                }
            });
        }
        return group;
    },

然后我定义

var service = new Service();

通过ajax请求获取它,然后将其应用于对象

function mapObjectToService(json){
    service = JSON.parse(json);
    service.__proto__= Service.prototype;
    $.each(service.groups, function(){
        this.__proto__ = sGroup.prototype;
        $.each(this.lines, function(){
            this.__proto__ = SupportLine.prototype;
        });
    });

    if (service.email != null){
        service.email.__proto__= sEmail.prototype;
    }else{
        service.email = new sEmail();
    }

    if (service.email.id == null){
        service.useSystemEmail = true;
    }else{
        service.useSystemEmail = false;
    }
}

当我在IE8中调用service.getGroup方法时,它失败并出现错误" Object不支持此属性或方法"。

group = service.getGroup(id)

在所有其他浏览器中,它可以正常工作。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我不认为所有浏览器支持__proto__,您可以创建一个通用工厂来从json数据创建对象:

//gets an item from an array of objects where key is value
//  could make both key and val an array if you want to match multi 
//  key/vals
//  returns an array if multi is true containing the indexes or empty 
//  array if not found
//  if multi is not true the index of the found item or -1 is returned
var getItem=function getItem(arr,key,val,multi){
  var i = arr.length,ret=[];
  while(--i>-1){
    if(arr[i][key]===val){
      if(multi){
        ret.push(i);
      }else{
        return i;
      }
    }
    return multi?i:ret;
  }
}
var Service = function Service(){};
Service.fromObject = function fromObject(obj){
  var key,hasOwn=Object.prototype.hasOwnProperty,i,
          ret=new Service();
  for(key in obj){
    if(hasOwn.call(obj,key)){
      ret[key]=obj[key];
    }
  }
  i=(ret.groups&&ret.groups.length)
    ||0;//if service has no groups no error will be thrown
  while(--i>-1){
    ret.groups[i]=Group.fromObject(ret.groups[i]);//same as 
       //Service.fromObject let the Group.fromObject take
       //  care of the lines same as this takes care of groups
  }
  ret.groups=ret.groups||[];//making sure ret.groups has a .length 
    //even if groups was not set
  ret.email=Email.fromObject(ret.email);
  ret.useSystemEmail=ret.email.id===null?false:true;
  return ret;//return the service
};
//getting an object from an array by key and value is a task you'll probably
//  do more than once so it is better to use a general function for this
Service.prototype.getGroup = function(id){
  var index = getItem(this.groups,'id',id);
  if(index!==-1){
    return this.groups[index];
  }
  return null;
};
var service = new Service(JSON.parse(json));

答案 1 :(得分:0)

感谢。我像你一样解决了它,但有点不同。

在IE8支持的构造函数中声明__proto__非常重要。

function Service(obj){
    this.__proto__ = Service.prototype;
    if (obj){
        for(var prop in obj){
            if (this.__proto__.hasOwnProperty(prop) && prop != 'groups' && prop != 'email'){
                this[prop] = obj[prop];
            }
        }
        if(obj.groups.length > 0){
            this.groups = [];
            for(var i = 0; i < obj.groups.length; i++){
                this.groups.push(new sGroup(obj.groups[i]));
            }
        }
        if(typeof(obj.email) != 'undefined' && obj.email != null){
            this.email = new sEmail(obj.email);
            this.useSystemEmail = false;
        }
    }
}

Service.prototype = {
//...
    }

然后在$ .ajax.success中定义新的服务

var obj = JSON.parse(data);
service = new Service(obj);