JS: What is instance property

By Xah Lee. Date: . Last updated: .

Instance properties of constructor C, are predefined own property keys of the objects returned by the constructor call.

the main point is that any object created by that constructor always have these own properties.

Instance properties are also known as Instance Fields.

πŸ›‘ warning: values of instance properties may be functions, so you can say they are instance methods but that phrase usually means the methods in the object's prototype.

Example. creating constructor with instance fields

class Burger {
 // these are instance fields
 cheese = 1;
 meat = 1;
 bread = 1;

 // this is instance method that lives on the prototype
 make_cheese() {}
}

// s------------------------------

// create a instance

const xx = new Burger();

// show instance fields
console.log(Reflect.ownKeys(xx));
// [ "cheese", "meat", "bread" ]

Example of instance properties