Javascript instanceof运算符返回不同的结果

时间:2017-11-02 13:30:34

标签: javascript class ecmascript-6

所以,我把这个CustomerProfile类作为:

 import _customer from '../swaggerCodegen/Customer'
 import Address from '../Address'

 export default class CustomerProfile {
   constructor(customerProfileResponse) {
       _customer.constructFromObject(customerProfileResponse,this)     
   }
 }

其中constructFromObject是由Swagger创建的静态函数:

static constructFromObject(data, obj) {
        if (data) {
            obj = obj || new Customer();


            if (data.hasOwnProperty('accountManager')) {
                obj['accountManager'] = Employee.constructFromObject(data['accountManager']);
            }
            if (data.hasOwnProperty('addresses')) {
                obj['addresses'] = ApiClient.convertToType(data['addresses'], [Address]);
            }
            if (data.hasOwnProperty('billingAccounts')) {
                obj['billingAccounts'] = ApiClient.convertToType(data['billingAccounts'], [BillingAccount]);
            }
            if (data.hasOwnProperty('billingStatus')) {
                obj['billingStatus'] = ApiClient.convertToType(data['billingStatus'], 'String');
            }
            if (data.hasOwnProperty('busNumber')) {
                obj['busNumber'] = ApiClient.convertToType(data['busNumber'], 'String');
            }
            if (data.hasOwnProperty('companyName')) {
                obj['companyName'] = ApiClient.convertToType(data['companyName'], 'String');
            }
            if (data.hasOwnProperty('contacts')) {
                obj['contacts'] = ApiClient.convertToType(data['contacts'], [Contact]);
            }
            if (data.hasOwnProperty('customerCode')) {
                obj['customerCode'] = ApiClient.convertToType(data['customerCode'], 'String');
            }
            if (data.hasOwnProperty('email')) {
                obj['email'] = ApiClient.convertToType(data['email'], 'String');
            }
            if (data.hasOwnProperty('gsm')) {
                obj['gsm'] = ApiClient.convertToType(data['gsm'], 'String');
            }
            if (data.hasOwnProperty('idNumber')) {
                obj['idNumber'] = ApiClient.convertToType(data['idNumber'], 'String');
            }
            if (data.hasOwnProperty('segment')) {
                obj['segment'] = ApiClient.convertToType(data['segment'], 'String');
            }
            if (data.hasOwnProperty('subsegment')) {
                obj['subsegment'] = ApiClient.convertToType(data['subsegment'], 'String');
            }
        }
        return obj;
    }

此外,这里是在covertToType函数中处理数组的代码片段:

default:
                if (type === Object) {
                    // generic object, return directly
                    return data;
                } else if (typeof type === 'function') {
                    // for model type like: User
                    return type.constructFromObject(data);
                } else if (Array.isArray(type)) {
                    // for array type like: ['String']
                    var itemType = type[0];

                    return data.map((item) => {
                        return ApiClient.convertToType(item, itemType);
                    });
                } else if (typeof type === 'object') {
                    // for plain object type like: {'String': 'Integer'}
                    var keyType, valueType;
                    for (var k in type) {
                        if (type.hasOwnProperty(k)) {
                            keyType = k;
                            valueType = type[k];
                            break;
                        }
                    }

                    var result = {};
                    for (var k in data) {
                        if (data.hasOwnProperty(k)) {
                            var key = ApiClient.convertToType(k, keyType);
                            var value = ApiClient.convertToType(data[k], valueType);
                            result[key] = value;
                        }
                    }

                    return result;

从静态方法调用时,记录obj['addresses'][0] instanceof Address会返回 true ,而从CustomerProfile构造函数中记录this.addresses[0] instanceof Address会为我带来 false

任何见解?感谢

P.S调用constructor.name调用返回'地址'

0 个答案:

没有答案