0

Need help with this JavaScript question:

Now create three new objects. Each object should have the following properties:

  • type: a string specifying what type of mysticalAnimal this is. Unicorns and dinosaurs and yeti and Loch Ness Monsters and polar bears and red pandas are all viable options!
  • collects: an array with a few things this animal might collect
  • canFly: a boolean (true or false — no strings around it, these are reserved keywords) representing whether this animal can fly or not.

Our small paired programming group tried:

var myArray = [];
myArray.push(myObject);
var chipotle = ['unicorn', 'food', true];

How would we properly address this? What is the correct code?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Franklinc
  • 423
  • 1
  • 3
  • 11
  • I read it, but know what you're looking for exactly. You need post of clearer question, either that or I don't get it. – Script47 Aug 06 '15 at 02:15
  • 2
    That's not an object. An object has curly braces, keys, and values. If it is a custom type, use its constructor. – Paul Aug 06 '15 at 02:15
  • `'food'` is not an array but a string. – Sebastian Simon Aug 06 '15 at 02:19
  • I guess the question is how do you create a object with three properties and then add that object into an array correctly. So all the code together. – Franklinc Aug 06 '15 at 02:53

2 Answers2

2

Here is an example.

An object looks like this:

var Rudolph = {
   type: 'magic reindeer',
   collects: ['insults','sleigh','toys'],
   canFly: true
 }

The above code creates an Object. Objects are very general, the keys are effectively strings and the values can be a string, a number, or even an arrays or another object.

If you are studying custom classes in Javascript, there might be a constructor function that you are supposed to write, or use.

If you had, or are given, a function

function mysticalAnimal(type, collects, canFly){
    this.type = type;
    this.collects = collects;
    this.canFly = canFly;
}

then you could use

var Rudolph = new mysticalAnimal('magic reindeer', 
                                 ['insults','sleigh','toys'],
                                 true);

to create an object that is an instance of mysticalAnimal.

One advantage of this is that the custom function-based object's origin can be tested with:

Rudolph instanceof mysticalAnimal
---> true
Paul
  • 26,170
  • 12
  • 85
  • 119
  • @Franklinc sounds like another question. Stack Overflow is an attempt to build a library of questions and answers, not answer all of a visitor's questions in one spot. Adding an object to an array is answered here: http://stackoverflow.com/questions/6254050/how-to-add-an-object-into-an-array – Paul Aug 06 '15 at 03:03
  • Thank you again for your assistance on this awhile back. Definitely was just starting. I didn't even know how to select the check mark on Stack Overflow to show the question as answered. Also, my question phrased here was horrid. Thanks again for the time you spent! – Franklinc Sep 06 '16 at 18:58
0

See Object - MDN

You may create one like this:

var obj = {
  type: 'Unicorns',
  collects: ['stuff1', 'stuff2'],
  canFly: true
}
iplus26
  • 2,518
  • 15
  • 26