如何在Javascript中创建对象以及更改其属性的方法?

如何在Javascript中创建对象以及更改其属性的方法?,javascript,Javascript,我正在学习Javascript并尝试完成以下练习: “编写一个脚本,为名为Ani、Sipho、Tuulia、Aolani、Hiro和Xue的人创建对象,以便: 图利亚是西福的母亲。 阿尼和西福结婚了。 Ani和Sipho的子女依次为Aolani、Hiro和Xue。 使用您可以填写的尽可能多的以下属性定义每个person对象:姓名、母亲、父亲、配偶和子女。childrenproperty应具有数组值。还要为person对象创建允许更改配偶属性的方法 console.log(sipho.moth

我正在学习Javascript并尝试完成以下练习:

“编写一个脚本,为名为Ani、Sipho、Tuulia、Aolani、Hiro和Xue的人创建对象,以便:

图利亚是西福的母亲。 阿尼和西福结婚了。 Ani和Sipho的子女依次为Aolani、Hiro和Xue。 使用您可以填写的尽可能多的以下属性定义每个person对象:姓名、母亲、父亲、配偶和子女。childrenproperty应具有数组值。还要为person对象创建允许更改配偶属性的方法

  console.log(sipho.mother); 
  // tuulia
  ani.changeSpouse("mars");
  console.log(ani.spouse);
  // mars"
到目前为止,我掌握的代码是:

        var ani = {name: 'Ani', children:[]};
        var tuulia = {name: 'Tuulia'};
        var sipho = {name: 'Sipho', mother: 'Tuulia', spouse: 'Ani', children:[]};
        var aolani = {name: 'Aolani', mother: 'Ani', father: 'Sipho'};
        var hiro = {name: 'Hiro', mother: 'Ani', father: 'Sipho'};
        var xue = {name: 'Xue', mother: 'Ani', father: 'Sipho'};
        
        this.changeSpouse = function (spouse) {
            this.spouse = name;
        }

        console.log(sipho.mother); 
        ani.changeSpouse("mars");
        console.log(ani.spouse);
所以,现在我能够获得console.log(sipho.mother)的母名称

我在创建更改配偶姓名的函数时遇到问题。有人能指出我在这方面的错误吗

另外,我也不完全确定我是否在做“childrenproperty应该有一个数组值”。正确。

一些评论:

#1-您正确地实现了children属性。它是一个数组,用于在其中存储个人的孩子

#2-我不确定您的练习是否要求您存储一个字符串,其中包含人员的姓名、母亲、父亲等,或者实际的母亲/父亲对象本身(更具体地说是指向该对象的指针,因为对象是引用类型) i、 e

#3-由于您在全局范围内声明了ChangeDomber函数,因此您在使用ChangeDomber函数时遇到了问题。如果您希望ani有一个方法ChangeDomber,您需要像这样将其移动到ani对象中

var ani = {
    name:'Ani', 
    children:[], 
    changeSpouse: function (spouse) {
        this.spouse = spouse;
    }
<P>4——如果你重复制作相同结构的对象,考虑创建一个类。你可能还没有了解它们,如果是的话,不用担心。下面是一个例子实现< /P>
class Person {
    constructor(name, mother, father, children=[], spouse=null) {
        this.name = name;
        this.mother = mother;
        this.father = father;
        this.children = children;
        this.spouse = spouse;
    }

    changeSpouse(spouse) {
        this.spouse = spouse;
    }
}
并声明一个新实例

let ani = new Person('Ani', /*other parameters go here*/);
有几点意见:

#1-您正确地实现了children属性。它是一个数组,用于在其中存储个人的孩子

#2-我不确定您的练习是否要求您存储一个字符串,其中包含人员的姓名、母亲、父亲等,或者实际的母亲/父亲对象本身(更具体地说是指向该对象的指针,因为对象是引用类型) i、 e

#3-由于您在全局范围内声明了ChangeDomber函数,因此您在使用ChangeDomber函数时遇到了问题。如果您希望ani有一个方法ChangeDomber,您需要像这样将其移动到ani对象中

var ani = {
    name:'Ani', 
    children:[], 
    changeSpouse: function (spouse) {
        this.spouse = spouse;
    }
<P>4——如果你重复制作相同结构的对象,考虑创建一个类。你可能还没有了解它们,如果是的话,不用担心。下面是一个例子实现< /P>
class Person {
    constructor(name, mother, father, children=[], spouse=null) {
        this.name = name;
        this.mother = mother;
        this.father = father;
        this.children = children;
        this.spouse = spouse;
    }

    changeSpouse(spouse) {
        this.spouse = spouse;
    }
}
并声明一个新实例

let ani = new Person('Ani', /*other parameters go here*/);

请考虑对你所做的事情做一个简短的描述,请考虑对你所做的事情做一个简短的描述。