JavaScript creating new instance of objects

javascript object constructor
javascript object methods
object.create vs new
javascript object properties
javascript new
javascript class
object object javascript
javascript prototype

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.

Object.create(), Your existing code: // Creates a new, empty object, as a global course = new Object(); // Creates three new variables in the global scope. var  The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function. The new keyword does the following things: Creates a blank, plain JavaScript object; Links (sets the constructor of) this object to another object; Passes the newly created object from Step 1 as the this context;

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:

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

new operator, Literally typing code line-by-line to create an instance of an object }//CREATE A NEW INSTANCE AND LOG 'Guardian of the Galaxy' Creating new objects. JavaScript has a number of predefined objects. In addition, you can create your own objects. You can create an object using an object initializer. Alternatively, you can first create a constructor function and then instantiate an object invoking that function in conjunction with the new operator. Using object initializers

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

JavaScript creating new instance of objects, Creating objects using the 'new' keyword. This method of object creation resembles the way objects are created in class-based languages, like  So when I create a new Employee I just say: var employee1 = new Employee(); So then I can manipulate this instance of the object. Now, I want this objects to be created dynamically with the variable names: employee1, employee2, employee3 and so on.. Is there a way to achieve this or is it impossible?

Making Object Instances in JavaScript - Kamie Robinson, Using the Object() constructor: var d = new Object();. This is the simplest way to create an empty object. I believe it is now discouraged. JavaScript provides most of the functionality that create with prototypal instantiation with the use of the keyword new. Pseudoclassical instantiation utilizes this when creating a new object.

How to create objects in JavaScript, Create JavaScript Object with Constructor. Constructor is nothing but a function and with help of new keyword, constructor function allows to  Creating objects using the ‘new’ keyword This method of object creation resembles the way objects are created in class-based languages, like Java. By the way, starting with ES6, classes are native to JavaScript as well and we will look at creating objects by defining classes towards the end of this article.

Different ways of creating an Object in javascript (Example), It creates a fresh new object(instance) Automobile() and assigns it to a variable. It sets the constructor property i.e “color” of the object to Automobile. Object.create​  A primitive data type is data that has a primitive value. JavaScript defines 5 types of primitive data types: string. number. boolean. null. undefined. Primitive values are immutable (they are hardcoded and therefore cannot be changed). if x = 3.14, you can change the value of x.

Comments
  • Please read the basics here first: developer.mozilla.org/en-US/docs/JavaScript/Guide/….
  • Then how would I reference the fields of the course created with the function? The idea is I want to hold onto these like an instance of a class in java.
  • Thank you for your explanation! you saved me. I lost a couple of hours trying to fix a bug, the problem was I didn't understand the oo structure of javascript.
  • Even though both answers are correct, this one made more sense to me than the accepted answer.