【问题标题】:Difference between creating a class in javascript to create an object and creating an class and object in Java在javascript中创建类来创建对象和在Java中创建类和对象的区别
【发布时间】:2016-05-03 17:07:13
【问题描述】:

声明:

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;

【问题讨论】:

  • 对象的创建方式如下:var obj = {}。在javascript中(几乎)一切都是一个对象。类实例只是来自构造函数的特殊对象。您可以将 Object 视为 javascript 中所有内容的父类。

标签: javascript class object


【解决方案1】:

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

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

【讨论】:

  • 我会编写代码,但是您会更舒服,并且其他人更容易阅读。
【解决方案2】:

javascript中有类。但是您也可以使用 .prototype 属性继承对象属性。这个W3School's article可以更好的解释。

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

 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

【讨论】:

  • 嗯。你将如何在 javascript 中创建一个类?
【解决方案3】:

JavaScript 没有类的概念。无需从一开始就定义类,使用 JavaScript 您只需编写代码即可。

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

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

参考:面向对象 JavaScript 的原理,Nicholas C. Zakas

【讨论】:

    【解决方案4】:

    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
    

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

    使用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
    

    【讨论】:

    • 非常有帮助,非常感谢
    猜你喜欢
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2023-02-26
    相关资源
    最近更新 更多