JavaScript Object / Classes

JavaScript Object / Classes

本文关键字:Classes Object JavaScript      更新时间:2023-10-10

下面显示的两种不同类型的JavaScript类之间的差异:

第一个:

 var apple = new function() {
    this.type = "macintosh";
    this.color = "red";
    this.getInfo = function () {
        return this.color + ' ' + this.type + ' apple';
    };
}

第二个:

 var apple = ({
      type:'macintosh',
      color:'red',
      getInfo: function() {
       return type +' '+ color + 'apple';
      }
});

两者都在.js文件中定义。

第一个是一个可以从中创建对象的类

var test = new apple();

第二个是对象,您可以使用它的成员和方法