javascript - 理解javascript对象(不同类型的声明)

标签 javascript class object initialization

我试图理解 javascript 对象,但我很难理解不同类型的声明。

I read定义对象有两种主要方式

方法一:

var student1 = {
    name: "Peter Foti",
    course: 'JavaScript',
    grade: 'A',
    dispInfo: function(){
         return this.name + ' has an ' + this.grade; 
    }
};

方法2:

function student (a, b, c) {
    this.name = a;
    this.course= b;
    this.grade = c;
    this.dispInfo = function(){
         return this.name + ' has an ' + this.grade; 
    }
}

对于方法2,我理解这个概念,如果我想创建一个student类型的变量,我只需要调用:

student1 = new student("Jean Dupont", "wine tasting", "A");
console.log(student1.dispInfo);

但是,使用方法1,如何在不重新编写dispInfo等所有内部函数的情况下创建student2?

我想做类似的事情

var student2 = {
    name: "Olivier Perraut",
    course: 'Pétanque',
    grade: 'F'
};

console.log(student2.getInfo);

最佳答案

第一种方法是创建一个您只想拥有一个的对象。这是针对单例的。它直接创建 student1 对象。

第二个方法是构造函数。可以一次又一次地使用构造函数来创建所需数量的此类对象。

按照惯例,构造函数应该是初始上限的(例如,Student 而不是 student),就像 JavaScript 自己的对象(DateRegExp,...)。

您可以使用 JavaScript 原型(prototype)链,以便所有 Student 对象使用相同的 dispInfo 函数(具有不同的 this 值),而不是而不是为每个对象创建一个 dispInfo:

function Student (a, b, c) {
    this.name   = a;
    this.course = b;
    this.grade  = c;
}
Student.prototype.dispInfo = function(){
     return this.name + ' has an ' + this.grade; 
};

var s1 = new Student("Mary", "Algebra", "A");
var s2 = new Student("Joe", "Classical Sculpture", "B+");

从 ES5 开始(对于旧版浏览器,这也可以通过“shims”实现),您不必使用构造函数来拥有共享原型(prototype)的对象,您可以使用 Object.create要做到这一点。我更喜欢构造函数,但您也可以使用构建器函数:

var StudentPrototype = {
    dispInfo: function(){
        return this.name + ' has an ' + this.grade; 
    }
};
function BuildStudent(a, b, c) {
    var student    = Object.create(StudentPrototype);
    student.name   = a;
    student.course = b;
    student.grade  = c;
    return student;
}
var s1 = BuildStudent("Mary", "Algebra", "A");
var s2 = BuildStudent("Joe", "Classical Sculpture", "B+");

请注意,我们new与构建器函数一起使用,而仅与构造函数一起使用。 (如果您这样做通常是无害的,但这是不必要的,并且会误导任何阅读代码的人,因此您不想这样做。)

或者在这种简单的情况下你甚至不需要构建器函数,你可以直接使用Object.create,但这有点麻烦,因为如果你传入属性描述符(第二个参数),每个必须是一个描述属性的对象,而不仅仅是它的值(这是有充分理由的),所以你必须这样做 {value: "the value"} (当然,您可能想要指定有关该属性的其他信息,例如它是否可枚举等):

var StudentPrototype = {
    dispInfo: function(){
         return this.name + ' has an ' + this.grade; 
    }
};
var s1 = Object.create(StudentPrototype, {
        name:   {value: "Mary"},
        course: {value: "Algebra"},
        grade:  {value: "A"}
});
var s2 = Object.create(StudentPrototype, {
        name:   {value: "Joe"},
        course: {value: "Classical Sculpture"},
        grade:  {value: "B+"}
});

就我个人而言,我更喜欢构造函数,但 JavaScript 的伟大之处在于它支持多种编程风格,包括诸如构建器或直接使用 Object.create 等更合适的编程风格。

关于javascript - 理解javascript对象(不同类型的声明),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16250342/

相关文章:

java - 如何在 Java 线程上使用共享内存?

c++ - 在 c 文件中使用 C++ 类

ios - removeObjectAtIndex : is removing data from the main array and duplicate array as well

javascript - 使用 TypeScript 1.5 的 Angular 2 服务注入(inject)

javascript - Webpack 无法解析 html-loader

javascript - 当parent.DIV未悬停时,停止DIV跟随光标

Java 以 C# 中的样式初始化类

arrays - 不同类型的对象数组的 Typescript 类型

c++ - 如何通过复制内存序列化 C++ 中的对象?

javascript - 从小时计算剩余的年/天/小时/分钟/秒