1

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;

1 Answer 1

1

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
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the info. It sounds like I shouldn't solve my problem by using Object.prototype. What is a better solution? Towards the bottom of my code sample above, you can see "goal". That's what I'm trying to accomplish. Any help would be greatly appreciated.
Make it a static function: multiparter(mekBldr.mecha, stuck2).head01.cost not a method. One day you'd encounter an object that had its own multiparter property, and your code would break.
Like this? codefunction multiparter(theObj,route) { route.forEach(function (key) { theObj = theObj[key] }); return theObj; }code
@user1592980 Yes, exactly. Though I recommend to use reduce instead of forEach like in my answer :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.