user1592980 user1592980 - 1 month ago 21
jQuery Question

Object Prototype Function Breaking in jQuery

Someone helped me with a great object prototype for JavaScript but it breaks in jQuery. In jQuery it gives an error:

jquery.min.js:2 Uncaught TypeError: V[g].exec is not a function

I found that the Object.defineProperty block (below) stops the jQuery error. But then it doesn't work. When I call to multiparter() it just returns "undefined". Can anyone help with a solution?

Object.prototype.multiparter = function(route) {
var newObj = this;
route.forEach(function(key) {
newObj = newObj[key]
});
return newObj;
};

Object.defineProperty(Object.prototype, 'multiparter',{
value : function() {},
enumerable : false
});

var mekBldr = {
mecha: {
heads: {
head01: {
cost: 0,
classification: ''
}
},
rightLimbs: {
legs: {
rightleg01: {
cost: 0,
classification: ''
}
}
},
leftLimbs: {
legs: {
leftleg01: {
cost: 0,
classification: ''
}
}
}
}
};
var part = 'heads';
var stuck2 = [part];
var part1 = 'leftLimbs';
var part2 = 'legs';
var stuck = [part1, part2];
mekBldr.mecha.multiparter(stuck2).head01.cost = 2;
//goal: mekBldr.mecha.leftLimbs.legs.leftleg01.cost = 5;
mekBldr.mecha.multiparter(stuck).leftleg01.cost = 5;

Answer

By passing a value to the descriptor, you've just overwritten the method with that empty function (which is the one doing nothing and returning undefined). If you really want to define a method on Object.prototype (which you absolutely should not do), you'd need to use

Object.defineProperty(Object.prototype, 'multiparter', {
    value: function(route) {
        return route.reduce(function(newObj, key) {
            return newObj[key]
        }, this);
    },
    enumerable: false,
    configurable: true
});