我可以在javascript中使用与class相同的对象吗?

时间:2022-11-25 07:55:37

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

我可以在javascript中使用与class相同的对象吗?

3 个解决方案

#1


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

javascript中没有类本身,只有构建对象的方法。

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:

如果你声明一个名为bob的变量,那么会发生什么:

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.

在这种情况下,将调用函数bob,创建对象,以及由新变量bob破坏的函数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";
})();

#2


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.)

您可以对类和变量使用相同的名称,是的。但是以大写字母开始该类并将变量名称保持为小写。 (因此鲍勃和变量bob类。)

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

Javascript区分大小写,因此它知道区别。对你来说,两者都会读到相同的内容。

#3


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

那么jQuery:$('string'),我能说的函数,以及$ .ajax,一个名为ajax的方法的类。

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

名为$的函数和名为$的类。我知道我错了,但这就是它的样子。

#1


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

javascript中没有类本身,只有构建对象的方法。

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:

如果你声明一个名为bob的变量,那么会发生什么:

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.

在这种情况下,将调用函数bob,创建对象,以及由新变量bob破坏的函数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";
})();

#2


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.)

您可以对类和变量使用相同的名称,是的。但是以大写字母开始该类并将变量名称保持为小写。 (因此鲍勃和变量bob类。)

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

Javascript区分大小写,因此它知道区别。对你来说,两者都会读到相同的内容。

#3


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

那么jQuery:$('string'),我能说的函数,以及$ .ajax,一个名为ajax的方法的类。

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

名为$的函数和名为$的类。我知道我错了,但这就是它的样子。