ClassIWrote is not a constructor

Why get TypeError: ClassIWrote is not a constructor ? How solve this?

let ClassIWrote = { 
    someMethod: function() {return "This some text";}  
}


const myObject = new ClassIWrote();

myObject.someMethod();

Should I wrote constructor?
but this case i get function statements require a function name

function ClassIWrote2(){
  someMethod: function() {return "This some text";}  
}

Are you trying to use JS class syntax here? If so, you are missing the word class in the definition and you are using the wrong syntax in the method definition. Refer to the MDN docs on classes for how to do it.

1 Like

Look at this too:

you will find how to creat a Constructor Function, if that’s what you’re looking for

1 Like

Intersting this part missing from the curiculum of JavaScript Algorithms and Data Structures / Object Oriented Programiing

or I not notice?

Thank you very much

No, there’s an entire section of the curriculum on dealing with objects (the OO programming section), then class syntax, which does the same thing, is covered in a few lessons in the ES6 section.

let ClassIWrote = { 
    someMethod: function() {return "This some text";}  
}

This is an object. You can’t use new on an object because it’s already an object. new is used to create an object from a function call - new myFunction() will try to create an object based on whatever myFunction does.

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