Javascript 如何使用参数创建对象的属性,而不必使每个参数相等

Javascript 如何使用参数创建对象的属性,而不必使每个参数相等,javascript,object,constructor,Javascript,Object,Constructor,我有一个构造函数,它创建具有属性的电缆。我想将构造函数的参数分配给同名属性的值 function SimpleCable(X, Y, totalradius, outerradius, innerradius, Type, name) { this.Center = [X, Y]; this.TotalRadius = totalradius; this.ConductorOuterRadiusList = outerradius; this.ConductorInnerRadi

我有一个构造函数,它创建具有属性的电缆。我想将构造函数的参数分配给同名属性的值

function SimpleCable(X, Y, totalradius, outerradius, innerradius, Type, name) {
  this.Center = [X, Y];
  this.TotalRadius = totalradius;
  this.ConductorOuterRadiusList = outerradius;
  this.ConductorInnerRadiusList = innerradius;
  this.Type = Type;
  this.Name = name;
}
是否可以直接分配名称和类型参数,而不必进行明显的相等

我曾经想过这样的事情,但是它不起作用

function SimpleCable(X, Y, totalradius, outerradius, innerradius, Type, name) {
  this.Center = [X, Y];
  this.TotalRadius = totalradius;
  this.ConductorOuterRadiusList = outerradius;
  this.ConductorInnerRadiusList = innerradius;
  this.type;
  this.name;
}

谢谢大家!

如果希望缩短构造函数初始化时间,则可以使用and(但请注意,这是and
ES6
功能,因此建议在使用它之前使用and)来创建与参数同名的属性,如下所示:

function SimpleCable(X, Y, totalRadius, outerRadius, innerRadius, type, name)
{
    Object.assign(
      this,
      {center: [X, Y], totalRadius, outerRadius, innerRadius, type, name}
    );
}
class SimpleCable{
    constructor(options){
        Object.keys(options).forEach(name=>{
            this[name]=options[name];
        });
    }
}
new SimpleCable({center:[5,7], totalRadius:3});
例子
函数SimpleCable(X,Y,totalRadius,outerRadius,innerRadius,type,name)
{
Object.assign(
这
{中心:[X,Y],总半径,外半径,内半径,类型,名称}
);
}
设cable1=新的SimpleTable(1,2,4,5,4,“类型1”,“名称1”);
日志(“cable1为:”,cable1);
设cable2=新的简单表(7,6,7,3,2,“类型2”,“名称2”);
日志(“cable2为:”,cable2)
.as控制台{背景色:黑色!重要;颜色:石灰;}

.as console wrapper{max height:100%!important;top:0;}
如果希望缩短构造函数初始化,则可以使用and(但请注意这是and
ES6
功能,因此建议在使用它之前使用and)来创建与参数同名的属性,如下所示:

function SimpleCable(X, Y, totalRadius, outerRadius, innerRadius, type, name)
{
    Object.assign(
      this,
      {center: [X, Y], totalRadius, outerRadius, innerRadius, type, name}
    );
}
class SimpleCable{
    constructor(options){
        Object.keys(options).forEach(name=>{
            this[name]=options[name];
        });
    }
}
new SimpleCable({center:[5,7], totalRadius:3});
例子
函数SimpleCable(X,Y,totalRadius,outerRadius,innerRadius,type,name)
{
Object.assign(
这
{中心:[X,Y],总半径,外半径,内半径,类型,名称}
);
}
设cable1=新的SimpleTable(1,2,4,5,4,“类型1”,“名称1”);
日志(“cable1为:”,cable1);
设cable2=新的简单表(7,6,7,3,2,“类型2”,“名称2”);
日志(“cable2为:”,cable2)
.as控制台{背景色:黑色!重要;颜色:石灰;}

.作为控制台包装{max height:100%!important;top:0;}
您可以这样做:

function SimpleCable(X, Y, totalRadius, outerRadius, innerRadius, type, name)
{
    Object.assign(
      this,
      {center: [X, Y], totalRadius, outerRadius, innerRadius, type, name}
    );
}
class SimpleCable{
    constructor(options){
        Object.keys(options).forEach(name=>{
            this[name]=options[name];
        });
    }
}
new SimpleCable({center:[5,7], totalRadius:3});

你可以这样做:

function SimpleCable(X, Y, totalRadius, outerRadius, innerRadius, type, name)
{
    Object.assign(
      this,
      {center: [X, Y], totalRadius, outerRadius, innerRadius, type, name}
    );
}
class SimpleCable{
    constructor(options){
        Object.keys(options).forEach(name=>{
            this[name]=options[name];
        });
    }
}
new SimpleCable({center:[5,7], totalRadius:3});

但这些都不是“相同的名字”。JavaScript关心大小写。不管大小写问题如何,JavaScript都不会以这种方式运行。构造函数的设计目的是帮助我们初始化对象,包括为成员属性编写简单赋值语句的负担,但它们不是“相同的名称”。JavaScript关心大小写。不管大小写问题如何,JavaScript都不会以这种方式运行。构造函数旨在帮助我们初始化对象,包括为成员属性编写简单赋值语句的负担。