简体   繁体   中英

Writing a function that takes 2 separate constructor functions as its parameters using JavaScript

I am relearning JavaScript and I have run into an issue that I can not figure out. If someone has the answer to this and could give me an explanation as to where I have gone wrong I would greatly appreciate it.

I have 2 functions, 1 is called Person with the parameters (name, petProp). I set petProp to look at the second function which is called Pet with the parameters of (name, species).

I then create a new Person and a new Pet.

Next I create a function which will assign the pet to the person. I want to be able to add new people and new pets to this.

Lastly I want to write a reportPets function that takes a Person object as a parameter and outputs to the console the person's name followed colon followed by a comma-separated list of pets with the species of pet in parentheses after each pet.

My issue is the first 3 functions and getting them to work properly. I can get a console.log output which has 4 parameters where I am really only looking for 3. Here is the code and I do hope that these questions make sense.

 function Person(name, petProp) { this.name = name; this.petProp = Pet; } let myPerson = new Person("Mary"); function Pet(name, species) { this.name = name; this.species = species; } let myPet = new Pet("Fluffy", "Cat"); function assignPetToPerson(Pet, Person) { return this.myPet + " " + this.myPerson; } assignPetToPerson(myPet, myPerson); console.log(myPerson, myPet); 

Any help is greatly aprreciated to further my knowledge and understanding of this. Thank you.

You need to proofread carefully, and think of objects as unique items, not members of a class. You can add properties at any time.

You reference a nonexistent object Pet in your Person function. You pass in an argument petProp to that same function that you aren't doing anything with. You can just ignore the pet portion of things until you want to add it in the assignPetToPerson function.

Creating the functions and then creating the objects might help keep things clear.

 function Person(name) { this.name = name; } function Pet(name, species) { this.name = name; this.species = species; } function assignPetToPerson(Pet, Person) { Person.pet = Pet; } const myPet = new Pet("Fluffy", "Cat"); const myPerson = new Person("Mary"); assignPetToPerson(myPet, myPerson); console.log(myPerson); 

I assume you want to have myPet as the petProp of myPerson ?

This is (one of the many ways) how you can achieve that:

function Person(name) {
    this.name = name;

    this.setPet = function(pet) {
      this.petProp = pet;
    }
}

let myPerson = new Person("Mary");

function Pet(name, species) {
    this.name = name;
    this.species = species;
}

let myPet = new Pet("Fluffy", "Cat");

myPerson.setPet(myPet);

console.log('myPerson:', myPerson);
console.log('myPet:', myPet);

What the code does it to create a public method setPet for every Person instances. Instances are created by calling new Person() .

The public method can be called from the instance, so you can call myPerson.setPet(myPet) . The setPet function will set this.petProp according to the value of myPet .

this in this context refers to the instance itself. For example, if you are calling myPerson.setPet() , the this in setPet refers to myPerson . You can test this by accessing myPerson.petProp directly after setting the pet, the value will be the same.

BUT,

you actually don't need to create a public method for this purpose. You can just do myPerson.petProp = myPet . Javascript is very dynamic like that. But if you are coming from an OO background, this might be something you want to know that is possible in Javascript.

function Person(name, petProp) {
  this.name = name;
  this.petProp = Pet;
}

var myPerson = new Person("Mary");

function Pet(name, species) {
  this.name = name;
  this.species = species;
}

var myPet = new Pet("Fluffy", "Cat");

function assignPetToPerson() {
  return this.myPerson.name + ' ' + this.myPet.name + ' ' + this.myPet.species
}


console.log(assignPetToPerson());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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