1

I came to stackoverflow to find the answer to this question since I have found it to be a fabulous source of knowledge. And there indeed appears to be a lot of answers to this question but I am unable to find one that fits my needs.

After years of working on server side programming, I finally decided that a lot of stuff could be done a lot more quickly on the client side so I decided to jump more enthusiastically into JavaScript.

Since I was working for a customer I decided to try and do best practices right off the bat. So I searched for how to emulate namespaces and read up on "Classes" and so I evolved code as in what follows

var ABC {}; // Create a namespace.
ABC.Object1 = function() {} // A constructor for a class
ABC.Object1.prototype = {
    someMethod: function() {}
}

So now I can create an object by var o = new ABC.Object1() abd I can call o.someMethod()

But then I wanted to write s debug() method and in it I wanted to log some debug information. I am using Chrome for the browser and when I break and trace I can see that "o" is of type "ABC.Object1" but I can't find anyway where my method can determine the "class" name. I keep putting class in quotes because I know that there really is no such thing as a Javascript class. But it seems I should be able to do it. I noticed some methods use the name of the constructor Function, so I changed the constructor to

function ABC.Object1(){}

but that is bad syntax because there should be no period there.

I don't have that many classes, so I am happy to redo all the constructors but I'd like advice on how to do it so I can determine the name of the class and maintain the namespace idea. By calling everything under namespace ABC (not the real name, but illustrative I minimize the chance of a naming conflict.

One person suggested putting in the constructor "this.name = "ABC.Object1" and that would work but that is too hokey and certainly wouldn't scale.

Any advice much appreciated.

1
  • 1
    BTW, you're missing (optional) semicolons. Commented Aug 31, 2014 at 2:57

2 Answers 2

1

You need to create a named function expression:

ABC.Object1 = function Object1() {};

Note that the name of the function expression does not have anything to do with the name of the property.

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

3 Comments

+1 not only for a perfect answer but to put you at 399.505 so your avatar renders with a 400K :) More people should be aware of how functions actually can have names.
But then the name of the object would be Object1 and the name might conflict with other object names. Unless I missed your point, in which case I would ask for clarification
@MartinHorton: Actually, no; the name in a function expression does not create a global. Read kangax.github.io/nfe
0

I think you missed my point - if I defined it as:

ABC.Object1 = function Object1() {};

then the object might have a name of Object1 but then so would:

XYZ.Object1 = function Object1() {};

And so the advantage of the "namespace" is lost.

But to go back to my original post, when halted in the debugger the value of this is defined as ABC.Object1. How is the debugger determining this and is there a way for me to do the same?

Comments

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.