1

I am so confused with JavaScript object (as a C++ developer). How can this

var myObj = {key1: "Hello", key2: "World"};

be an instance of a class or is it just the name of a data structure ? Why do they even call it an object?

Assuming it is a class itself why use a colon instead of an equal sign (assuming the contents are variables)?

2
  • 2
    JavaScript does not have classes. It uses prototypical inheritance. Trying to think about it in terms of classes will cause more problems than it will solve. Commented Jul 2, 2020 at 18:57
  • I will google this thanks Commented Jul 2, 2020 at 18:57

2 Answers 2

1

Javascript is not a class inheritance based language. It is prototype inheritance based. When you create a new object with a set of curly braces it is essentially the same thing as doing this

var myObj = new Object()

Everything created in JavaScript originally inherits from Object

JavaScript after es6 does use keywords like class but its purely syntatic sugar. It is to help other developers get accustomed to JavaScript and its weird ways. Do some research on MDN all your answers are on there.

Sign up to request clarification or add additional context in comments.

1 Comment

JavaScript of course does feature classes, and the class syntax is not entirely just syntactic sugar. The capability of such a thing as a Class does vary from PL to PL as does the underlaying inheritance concept. JavaScript by now offers three syntactically different ways of building large object/type systems, each of course sitting on prototypes, delegation( and closures).
0

See MDN:

Nearly all objects in JavaScript are instances of Object; a typical object inherits properties (including methods) from Object.prototype, although these properties may be shadowed (a.k.a. overridden).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.