0

Can I have object with the same name as class in javascript?

5 Answers 5

4

There are no classes per se in javascript, only methods that build objects.

UPDATE: ES6, also known as ECMAScript2015, introduced classes.

To directly answer your question, yes and no. You can create a function that builds your object, but as soon as you have a variable of the same name, the function is destroyed.

there is no difference between

function bob() {
    //code goes here
    this.name = "bob";
}

and

var bob = function() {
    //code goes here
    this.name = "bob";
}

What would then happen if you declared a variable named bob like:

var bob = new bob();

In this case, the function bob would be called, the object created, and the function bob clobbered by the new variable bob.


If you want to create a singleton, then you might as well use a closure as follows:

var bob = new (function() {
    //code goes here
    this.name = "bob";
})();
Sign up to request clarification or add additional context in comments.

4 Comments

it's possible to do. the variable bob will now be an object that was created by the function bob. it's just that after that, the function bob will no longer be accessible (for invocation or instantiation)
@rakesh: just to add, whenever you are creating a variable name that is the exact same as your function name, you really need to ask yourself if it's necessary. It's quite common to name classes like SelectorPanel, and make instances named applicationSelectorPanel or layoutSelectorPanel (or just applicationPanel and layoutPanel). The class should be defining the kind (or class) of object to be created. The variable name should just be giving a meaningful label for what the object is.
@rakesh one more note (ran out of room on the last comment) you could even do something as simple as function Bob () { } and var bob= new Bob(); the two names would now be different due to a change in case of the first letter.
@Fingland, there's an error in the singleton example. It should either be var bob = new function(){ this.name = "Bob"; }; or var bob = {name : "Bob"}; -- the current version sets the "name" property of the global object, which you probably don't want.
1

What about jQuery: $('string'), a function as far as I can tell, and $.ajax, a class with a method named ajax.

Function named $ and class named $. I know I am wrong but this is what it looks like.

Comments

0

You can use the same name for class and variable, yes. But start the class with an uppercase letter and keep variable names lowercase. (Thus a class Bob and variable bob.)

Javascript is case sensitive so it knows the difference. For you, both would just read the same.

Comments

0

For this case, you should make your global function and other options of your specific function name like below:

var write = function(text) { console.log(text); }
write.hi = ()=>console.log("hi");
write.bye = ()=>console.log("bye");
write.word = (text)=>console.log(text);
write.feature = "Its a feature";
//...

write("baby");//'hello' printed
write.hi();//'hi' printed
write.bye();//'bye' printed
//...

Enjoy...

Comments

0

I'm answering assuming keeping in mind the best practises. Usually objects in JavaScript are defined as const. In such cases, having the same object name as class name will cause an error.

If the object is not defined as const, the new object will overwrite the class.

Either ways the outcome of attempting something like this is not good, so one should refrain from this practise and try keeping different name for classes and objects.

But if there is a special situation, you can try changing the Class name using TitleCasing and object name with camelCasing or any other variations.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.