gpt4 book ai didi

javascript - javascript中创建类创建对象与Java中创建类和对象的区别

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:07:26 25 4
gpt4 key购买 nike

声明:

Javascript can create an Object without creating a class.

该声明有效吗?也许我没有理解这里的概念...

例如,如果我在 Javascript 中创建一个对象:

var mercedes = new Object();
mercedes.speed = "260";
mercedes.model = "SLS";
mercedes.year = 1969;

这里我没有类,但我已经有一个实例。这是否意味着我不需要在 Javascript 中定义类?

还有一个问题,在 Javascript 中,如上所述创建对象与使用以下方法创建对象之间有什么区别:

myCar["speed"] = "260";
myCar["model"] = "SLS";
myCar["year"] = 1969;

最佳答案

javascript 中没有类。话虽如此,您可以使用几种不同的方式来模拟类(class)。

使用函数并为其原型(prototype)定义方法

var Person = function(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
};

// you can define Person.fullname = function() {} straight to the function
// drawback to this is fullname function will be created everytimg you create a new Person
// instead you can create fullname in the prototype of the Person so it only gets created once

Person.prototype.fullname = function() {
return this.firstName + ' ' + this.lastName;
};

var person = new Person('John', 'Doe');
person.fullname(); // John Doe

使用对象字面量

var Person = {
firstName: 'John',
lastName: 'Doe',
fullname: function() {
return this.firstName + ' ' + this.lastName;
}
};

Person.fullname(); // John Doe

Person.firstName = 'Jane';
Person.fullname(); // Jane Doe

使用单例

var person = new function() {
this.firstName = 'John';
this.lastName = 'Doe';
this.fullname = function() {
return this.firstName + ' ' + this.lastName;
};
};

person.fullname(); // John Doe

person.firstName = 'Jane';
person.fullname(); // Jane Doe

如果您打算更深入地了解 java 类的工作原理、应用继承等,那么我建议您使用第一种方法。下面是使用 prototype 的简单继承示例。

var Person = function(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
};

Person.prototype.fullname = function() {
return this.firstName + ' ' + this.lastName;
};

Person.prototype.getFirstName = function() {
return this.firstName;
};

Person.prototype.getLastName = function() {
return this.lastName;
};

Person.prototype.setLastName = function(lastName) {
return this.lastName = lastName;
};

Person.prototype.setFirstName = function(firstName) {
return this.firstName = firstName;
};

var Student = function(firstName, lastName, grade) {
this.grade = grade;
Person.call(this, firstName, lastName);
};

Student.prototype = Object.create(Person.prototype);

Student.prototype.getGrade = function() {
return this.grade;
};

var student = new Student('John', 'Doe', '5th');
student.fullname(); // John Doe
student.getFirstName(); // John
student.setFirstName('Jane');
student.getFirstName(); // Jane
student.getGrade(); // 5th

这应该解释了如何在 javascript 中使用类似于您在 java 中使用的类。

使用点符号括号访问对象属性有什么区别?

长答案简而言之,两者的行为完全相同。但在某些情况下,dot notation 并不总是有效。例如:

var person = {
"first name": "john",
"last name": "doe",
"fullname": "john doe"
};

person.fullname; // john doe

// to access first name property of person
person.first name; // will not work
person["first name"]; // john

关于javascript - javascript中创建类创建对象与Java中创建类和对象的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35025652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com