-2

as a beginner I tried to use classes in JavaScript because I started to learn programming with C#.

So i want to store a note (with a title and a text) to a data store. These are my small classes:

class Note{ // The single note
  constructor(noteTitle, noteText) { // set the notes title and text
    title = noteTitle;
    text = noteText;
  }

  var title;
  var text;
}

class Notestore{
  var notes = []; // stores all the notes

  function AddNote(note){
    notes.push(note); // add a new note to the list
  }
}

class NoteController{
  var store = new NoteStore(); // get the reference to the store

  function CreateNote(title, text){ // create a new note and store it
    store.AddNote(new Note(title, text));
  }
}

So when I start the application it says,

var store = new NoteStore();

is not defined. My NoteController is the first class getting called. What do I have to change, that my classes should work =?

Thanks for help!

Garzec
  • 69
  • 1
  • 1
  • 11
  • 2
    1. You don't declare fields in classes. So remove `var title;` andd others. 2. You use `this.` to address the instance properties, not just a name. It actually is surprising that you can run your code, since it is syntactically incorrect. – zerkms Mar 20 '17 at 22:11
  • `NoteStore !== Notestore` - and that's just the obvious typo – Jaromanda X Mar 20 '17 at 22:15
  • 1
    Possible duplicate of [What techniques can be used to define a class in JavaScript, and what are their trade-offs?](http://stackoverflow.com/questions/387707/what-techniques-can-be-used-to-define-a-class-in-javascript-and-what-are-their) – Inanc Gumus Mar 20 '17 at 22:20
  • well, that's solved then – Jaromanda X Mar 21 '17 at 05:21

3 Answers3

2

You are mistaken about how to use instance variables/class attributes in Javascript. You need to assign them to this (e.g. this.foo = 'bar'). You also made some mistakes about declaring methods, you do not need to use the function keyword. Here is your code, updated to properly use instance variables:

class Note { // The single note
  constructor(noteTitle, noteText) { // set the notes title and text
    this.title = noteTitle;
    this.text = noteText;
  }
}

class NoteStore {
  constructor() {
    this.notes = []; // stores all the notes
  }
  AddNote(note){
    this.notes.push(note); // add a new note to the list
  }
}

class NoteController {
  constructor() {
     this.store = new NoteStore(); // get the reference to the store
  }
  CreateNote(title, text){ // create a new note and store it
    this.store.AddNote(new Note(title, text));
  }
}

var store = new NoteStore();
Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • Also want to add that the OP should be using camelcase names for their methods, ie. addNote and createNote - not AddNote and CreateNote. – Nicole Stein Mar 20 '17 at 22:19
  • You are correct that is currently the trend to use camelCase for JS variables, functions and methods and UpperCamelCase for class names. I think this UpperCamelCase is common/popular in C#, Go, and a few other languages – Rob M. Mar 20 '17 at 22:22
0

First you call your class Notestore. Then you try to use it as NoteStore with a capital S.

Melvin
  • 1
  • 1
  • 2
0

Javascript is a case-sensitive language. Try changing

class Notestore{
  ...
}

to

class NoteStore{
  ...
}
mwarger
  • 821
  • 8
  • 7