简体   繁体   English

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

[英]How to create Objects in Javascript and methods to change its properties?

I'm learning Javascript and trying to complete the following exercise:我正在学习 Javascript 并尝试完成以下练习:

"Write a script that creates objects for people named Ani, Sipho, Tuulia, Aolani, Hiro, and Xue, such that: “编写一个脚本,为名为 Ani、Sipho、Tuulia、Aolani、Hiro 和 Xue 的人创建对象,这样:

Tuulia is the mother of Sipho. Tuulia 是 Sipho 的母亲。 Ani and Sipho are married. Ani 和 Sipho 结婚了。 The children of Ani and Sipho are, in order, Aolani, Hiro, and Xue. Ani和Sipho的孩子依次是Aolani、Hiro和Xue。 Define each of the person objects with as many of the following properties as you can fill in: name, mother, father, spouse, and children.使用尽可能多的以下属性定义每个人对象:姓名、母亲、父亲、配偶和子女。 The childrenproperty should have an array value. children 属性应该有一个数组值。 Also create a method for the person object that allows the spouse property to be changed.同时为object这个人创建一个方法,允许改变配偶的财产。

  console.log(sipho.mother); 
  // tuulia
  ani.changeSpouse("mars");
  console.log(ani.spouse);
  // mars"

The code I have so far is:我到目前为止的代码是:

        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);

so, right now I'm able to get the mother name for console.log(sipho.mother).所以,现在我可以得到 console.log(sipho.mother) 的母名。

I'm having trouble with creating a function to change the spouse name.我在创建 function 以更改配偶姓名时遇到问题。 Could anyone please point where I'm going wrong with this?谁能指出我哪里出了问题?

Also, I'm not entirely sure if I'm doing "the childrenproperty should have an array value."另外,我不完全确定我是否在做“childrenproperty 应该有一个数组值”。 correctly.正确。

A couple comments:一些评论:

#1 - You are implementing the children property correctly. #1 - 您正在正确实施 children 财产。 It is an array which stores the person's children inside it.它是一个数组,将此人的孩子存储在其中。

#2 - I'm not sure if your exercise requires you to store a string of the name of the persons mother father etc., or the actual mother/father object itself (more specifically a pointer to that object - since an object is a reference type) ie #2 - 我不确定你的练习是否需要你存储一个人的名字字符串等,或者实际的母亲/父亲 object 本身(更具体地说是指向 object 的指针 - 因为 object 是一个参考类型)即

var xue = {name:'Xue', mother:ani /*pointer to ani object*/, father: etc.

#3 - you are having trouble with the changeSpouse function because you are declaring it within the global scope. If you want ani to have a method changeSpouse, you need to move it into the ani object like so #3 - 你在使用 changeSpouse function 时遇到问题,因为你在全局 scope 中声明它。如果你想让 ani 有一个方法 changeSpouse,你需要像这样将它移动到 ani object

var ani = {
    name:'Ani', 
    children:[], 
    changeSpouse: function (spouse) {
        this.spouse = spouse;
    }

#4 - if you are repeatedly making objects of the same structure, consider creating a class. You may have not learnt about them yet, and if so, don't worry about it. #4 - 如果您重复制作具有相同结构的对象,请考虑创建一个 class。您可能还没有了解它们,如果已经了解,请不要担心。 Here is an example implementation这是一个示例实现

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;
    }
}

and to declare a new instance并声明一个新实例

let ani = new Person('Ani', /*other parameters go here*/);
function FamilyTree(name,mother,father,spouse,children) {
  this.name = name;
  this.mother = mother;
  this.father = father;
  this.spouse = spouse;
  this.children = children;
  this.changeSpouse = (newSpouse) => this.spouse = newSpouse; /* You can add 
that function here but its not necessery see below*/
}

let ani = new FamilyTree('ani');
let tuulia = new FamilyTree('Tuulia');
let sipho = new FamilyTree('Sipho','Tuulia',null,'Ani');
let aolani = new FamilyTree('Aolani','ani','Sipho');
let hiro = new FamilyTree('Hiro','Ani','Sipho');
let xue = new FamilyTree('Xue','Ani','Sipho');

// ani.spouse = 'Mars';
// console.log(ani);

/* you can achieve it this way ^^^ which is much simpler :) 

but below is how the method would work */ 

ani.changeSpouse('mars');
console.log(ani)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM