0

I am trying to add a namespace method to the Object prototype in javascript.

What I would like to be able to do is this:

var myObj = {}
myObj.namespace('nested.objects.are.created.if.not.present')

But I am getting lost. It seems quite easy to do a generic function, but not to add it to the protoype.

Here is what I have:

Object.prototype.namespace = function(ns_string) {
    var parts = ns_string.split('.');
    var parent = this;
    var i;
    var length = parts.length
    for (i = 0; i < length; i++) {
        // Create a property if it doesnt exist
        if (typeof parent[parts[i]] === "undefined") {
            parent[parts[i]] = {};
        }
        parent = parent[parts[i]];
    }
}

It appears that the value of parent is not being set correctly each time. Im sure its something very basic that I am missing, but Im not sure what it is.

Thanks in advance.

Richard

4
  • I just tried your code ( jsfiddle.net/TykH3 ) and it worked, however I don't think it's a good idea to modify the prototype for Object.. Commented Jul 17, 2012 at 21:32
  • You won't be able to use jQuery on your page if you do this. Commented Jul 17, 2012 at 21:39
  • @ByRichardPowell I think you might have misunderstood if someone actually said it's ok. Do you have any references? Commented Jul 17, 2012 at 21:41
  • @Esailija. I suspect you are right and I have mis-understood when it is ok to modify base prototypes (the prototypes that come with the language, not ones that are created). I will do some research tomorow, to make sure I understand better. Commented Jul 17, 2012 at 21:46

1 Answer 1

1

Ok first off, as Onkelborg said, this is not a good idea (I'd even go so far as to say it's a very bad idea). Adding properties to the prototypes of core objects is just asking for trouble. Let's say you add this method, and some code on your site (could be your's, could be from a library) does:

for (var key in {foo: 'bar'}) {
    // How many times will this iterate?
}

That loop should only iterate through once, getting 'foo' as a key. In practice however it will loop twice, because your "namespace" property will show up as a property.

With that being said, if you really want to do this anyway, your basic code should work. The only thing that could mess up the value of parent is if this was off, and for that to happen you would have to invoke the method with a different "this", using foo.namespace.call(bar) or something like that.

If you're not using call (or it's sibling, apply) everything should work.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the detailed explanation. For clarity's sake are you saying that adding to the prototypes of all core objects is a bad idea or just the Object prototype ? (sorry for the confusing terminology, hopefully it is still understandable)
Adding them to Object is particularly bad, but the same basic issue applies to any of them. If you want to learn more, this link goes in to a great amount of detail: perfectionkills.com/…
Thanks very much for this. The article and the articles linked have prooved to be an interesting read.

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.