Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8248841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:20:08+00:00 2026-06-07T23:20:08+00:00

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

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:20:11+00:00Added an answer on June 7, 2026 at 11:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to add my event handler to ComponentDispatcher.ThreadPreprocessMessage event like this: using System; using
Trying to add init parameter names to a list in init(ServletConfig) method. public void
Trying to add a class object into a List using reflection, but when invoking
I'm trying to add an extension method to my MVC 2 project without success
I am trying to call a class method dynamically depending on a condition. This
I tried to create this HtmlHelper method: namespace Power.WebUx.Helpers { public static class HtmlHelperExtensions
I have an abstract class which I would like to be able to expose
I'm trying to create a context menu which will be able to add extra
I am trying to use a method inside class, from another class. namespace Crystal.Utilities
I'm trying add data triggers to the default combobox style so each text item

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.