如何将对象用作构造函数?

时间:2019-02-28 03:20:03

标签: javascript

我是javascript新手,对如何将对象设置为构造函数有些困惑,所以可以说我有一些对象:

var avenger1 ={
    firstName : "Tony",
    lastName : "Stark",
    from : "New York",
    parents : {
        father : "Howard Stark",
        mother : "maria Stark"
    },
    getSummary : function(){
        return `${this.firstName} ${this.lastName} is a wealthy businessman from ${this.from}, born to  ${this.parents.father} dan ${this.parents.mother}`
    }
}

console.log(avenger1.getSummary())

我正在尝试将该对象构建为构造函数,这里是我的代码:

function Avenger(firstName, lastName,from, parents.father, parents.mother){
    this.firstName = firstName,
    this.lastName =  lastName,
    this.from = from,
    this.parents.father = parents.father,
    this.parents.mother = parents.mother,
    getSummary : function(){
        return `${this.firstName} ${this.lastName} is a wealthy businessman from ${this.from}, born to  ${this.parents.father} dan ${this.parents.mother}`
    }
}

var  Avenger1 = new Avenger("Tony", "Stark","New York", "Howard","Maria ");

我该如何设置像firstName等这样的父母,以免出错? 对不起,如果我的问题有点简单。

3 个答案:

答案 0 :(得分:2)

做的很棒!仅存在语法错误,其中可以删除逗号或将其替换为分号,方法应随附此方法,并且可以直接访问父级和父级参数。

function Avenger(firstName, lastName, from, father, mother){
    this.firstName = firstName;
    this.lastName =  lastName;
    this.from = from;
    this.parents = {
      father: father,
      mother: mother
    };
    this.getSummary = function(){
        return `${this.firstName} ${this.lastName} is a wealthy businessman from ${this.from}, born to  ${this.parents.father} dan ${this.parents.mother}`
    }
}

var  Avenger1 = new Avenger("Tony", "Stark","New York", "Howard","Maria ");

如果需要用不同的语言来收听它,MDN也是一种很好的资源,有时可以帮助我听到人们以两种不同的方式谈论它。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

答案 1 :(得分:1)

function Avenger(firstName, lastName, from, father, mother) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.from = from;
  this.parents = {
    father: father,
    mother: mother
  };
  this.getSummary = function() {
    return `${this.firstName} ${this.lastName} is a wealthy businessman from ${this.from}, born to ${this.parents.father} and ${this.parents.mother}.`;
  };
}

var avenger1 = new Avenger("Tony", "Stark", "New York", "Howard", "Maria");
console.log(avenger1.getSummary());

答案 2 :(得分:1)

您的代码有一些错误:

1)请勿在参数名称上使用dot.)。

2)使用semicolons;)结束句子(只是一个建议)

3)请勿使用:Avenger构造函数内部的变量赋值(参考:getSummary : function(){...})。 Avenger现在不是对象...

总而言之,您的代码可以修改为以下代码:

function Avenger(firstName, lastName, from, father, mother)
{
    this.firstName = firstName;
    this.lastName =  lastName;
    this.from = from;
    this.parents = {father, mother};
    this.getSummary = function()
    {
        return `${this.firstName} ${this.lastName} is a wealthy businessman from ${this.from}, born to  ${this.parents.father} dan ${this.parents.mother}`
    };
}

var  Avenger1 = new Avenger("Tony", "Stark","New York", "Howard","Maria ");
console.log(Avenger1.getSummary());
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}