Object constructor requires both parameters and curly braces?


#1

https://www.codecademy.com/courses/javascript-beginner-en-9Sgpi/2/3?curriculum_id=506324b3a7dffd00020bf661#

It doesn't look like its stated anywhere on the site or forums, but I can't move on to the next step in this exercise unless i include both curly {} and parameters around them ({}) after making my object. In forum posts, I see everyone using the stated method but I cant. see below:

//here is the only way my object will work
var me = new Object({});
me.name = 'Daniel';
me.age = 20;

// and here is what I am told the constructor is supposed to look like 
var myObj = new Object();

#2

- - - B a s i c s - - -

Each Object has one or more properties.
Each property consists of a property-key and it’s associated value.

 var object1 = {
              name: "First"
              }

So object1 has 1 property with property-key name
and it’s associated value “First” (which in this case is a “string-value”)
OR

var myObj = {
         type: 'fancy',
         disposition: 'sunny'
        }

myObj has 2 properties separated by a comma-,,
a property with property-key type and an associated-string-value 'fancy’
a disposition-property with property-key disposition and
…an associated-string-value ‘sunny’.

To create an Object,
you can use the literal notation,
you directly create an Instance of the object, with the
properties being separated by a comma-,

  var myObj = {
        type: 'fancy',
        disposition: 'sunny' 
  };

OR
You create an Object by the construct notation.
First you create an empty Object by way of either
myObj = new Object(); or myObj = {};
and then you attach its properties using the syntax
object-name.property-key = it’s associated value
thus:

  var myObj = {};
      myObj.type = 'fancy';
      myObj.disposition = 'sunny'; 

OR
There is also the facility Class construct notation.
The name should then start with a Capital-letter.

  var Person = function( theName, theAge ) {
         this.name = theName;
         this.age = theAge;
         this.displayInstance = function() {
           console.log("The displayInstance -output-"+
                       "\n============================" +
                     "\n\t name: " + this.name +
                     "\n\t age: " + this.age);
                       };
       };
    //now create an Instance of this Class-object
  var myObj = new Person("Classy_Rocker",20);
    //call the Method displayInstance which takes NO parameters
  myObj.displayInstance();
  console.log( myObj );

you can run this in labs repl.it try it !!

As you can see i created a function within this constructor,
they now call this function a Method.
So if in near future the course is asking you to create a method you now know
that you have to create
a property-key with an associated value being a function within an Object.


#3

thank you for the help, much appreciated!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.