JavaScript creating new instance of objects

So I am designing a grade book interface and I have a course defined as:

<script>
course = new Object();
 var name;
 var gradingareas;
 var finalgrade;
</script>

then later I want to create a new instance:

 var gradingareas = new Array("Homework", "Classwork", "Exams");

 course1 = new course("CS1500", gradingareas, 85);

I have also tried without the var in front to no avail. I get an “Uncaught TypeError: Object is not a function” I am very new to javascript so I don’t even know if Im going about this the correct way. Any help is appreciated Thanks.

Your existing code:

// Creates a new, empty object, as a global
course = new Object();
// Creates three new variables in the global scope.
var name;
var gradingareas;
var finalgrade;

There is no connection between the variables and the object.

It looks like you want something more like:

function Course(name, gradingareas, finalgrade) {
    this.name = name;
    this.gradingareas = gradingareas;
    this.finalgrade = finalgrade;
}

Then:

var course1 = new Course("CS1500", gradingareas, 85);

Note the use of a capital letter for naming the constructor function. This is a convention in the JS community.

JS is prototypical, rather than class based and if you are new to it there are advantages to learning this immediately rather than trying to mush classical inheritance models from it, however, classical inheritance is alive and well in JS.

Anyhow, to answer how you would access your variables:

course1.name works fine with the example above.

If you wanted to privatise your data you could take this approach using closure:

var Course = function(name, grade) {
  // Private data
  var private = {
    name: name,
    grade: grade
  }

  // Expose public API
  return {
    get: function( prop ) {
      if ( private.hasOwnProperty( prop ) ) {
        return private[ prop ];
      }
    }
  }
};

Then instantiate a new object:

Read More:   Query firestore database for document id

var course = new Course('Programming with JavaScript', 'A');

and start using all that private data:

course.get('name');

Of course, you’d probably want setters to manipulate that data too 😉

The code that you described does the following:

// Declares a memory variable called course and stores and object in it
var course = new Object();

// Declares three variables
 var name;
 var gradingareas;
 var finalgrade;

These declared variables aren’t automatically connected to the object. If you want these properties declared on the object you have 2 options:

  1. Declare them as properties of the object
  2. Declare them on the prototype of of the object

Example1: declare them as properties of the object:

// Declares a memory variable called course and stores and object in it
var course = new Object();

// Access or create new properties with . or [] operator
course.name="math";
course.gradingareas="muliple";
course['finalgrade'] = 'A'

console.log(course);

Example2: Declare them on the prototype:

// Create a constructor function
function Course (name, grade) {
  this.name = name;
  this.grade = grade;
}

// course is added on the prototype
Course.prototype.gradingareas="some gradingareas";

// the name and the grade are added on the object itself
var course = new Course ('willem', 10);

console.log(course);

To create a very simple object with constructor and default values, you can do :

//My object with constructor
var myObjectWithConstrutorFunction = {

    //construtor function with default values in constructor
    myConstrutor: function(Name="bob", Age = 18){
        this.Name = name;
        this.Age = age;
    }
};

// instance
var myInstance = new myObjectWithConstrutorFunction.myConstrutor();

// show on console
console.log('object with constructor function: ', myInstance);

// show properties 
console.log(myInstace.Name, myInstance.Age);

PS : It’s a good practice create a constructor’s name with the same name of the class, if you are creating a external class.

Read More:   Converting integers to hex string in JavaScript


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts