在 javascript 中创建类以创建对象与在 Java 中创建类和对象之间的区别

Difference between creating a class in javascript to create an object and creating an class and object in Java

本文关键字:创建 对象 之间 区别 创建对象 javascript Java      更新时间:2023-09-26

语句:

Javascript 可以在不创建类的情况下创建对象。

这种说法有效吗?也许我在这里的概念没有正确...

例如,如果我在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中没有类。话虽如此,您可以使用几种不同的方式模拟类。

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

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

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

使用 dot notationbrackets 访问对象属性有什么区别?

长话短说,两者的行为完全相同。但在某些情况下,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 中有类。但您也可以使用 .prototype 属性继承对象属性。这篇W3School的文章可以更好地解释它。

对于您的第二个问题,当您使用括号或点属性时没有真正的区别。使用变量访问对象属性时,使用括号访问属性可能很有用。例:

 var obj = {}:
 var val = 'value';
 obj[val] = 'new value';
 console.log(obj[val]); // Output: new value

以上也可以与:

var obj = { 'value' = 'new value'};
var val = 'value';
console.log(obj[val]); // Output: new value

这已经在SO上回答了好几次了,所以我将链接一个我在学习javascript时经常提到的非常好的答案:https://stackoverflow.com/a/387733/3299157

快速回答,是的,您可以在Javscript中创建对象,而无需创建传统的面向对象类。对于您的第二个问题,使用"括号样式"时没有明显的区别。

JavaScript 没有类的概念。而不是从一开始就定义类,使用 JavaScript 你可以只编写代码。

使用括号表示法,您可以动态决定要访问的属性,并且可以在属性名称中使用特殊字符。

var prop = "speed";
myCar = {};  // create a object
myCar[prop] = "260"; // using dynamically property
myCar["speed.a"] = "265"; // using special characters

参考文献:The Principles of Object-Oriented JavaScript, Nicholas C. Zakas