I know this is a late response, but it doesn't look like anyone followed up on your response:
we know that class is infact a shorthand for prototype, in constructor
method, we can instantilize the object. however, what i want is to
define a prop in its prototype within class definition but outside the
constructor method
You can just continue to declare the class as you did:
class Circle {
constructor (radius) {
this._radius = radius;
}
}
And then define the properties like this:
Object.defineProperties(obj, {
radius: {
get: function () {
return this._radius;
},
set: function (n) {
this._radius = n;
}
},
area: {
get: function () {
return Math.PI * this._radius * this._radius;
},
set: function (n) {
this._radius = Math.sqrt(n / Math.PI);
}
}
});
or whatever getters and setters you want.
I gave the actual _radius
member a leading underscore to differentiate it as the member variable separate from the radius
property being added, since they'd both be this.radius otherwise, leading to a stack overflow if you try to set it.
But you asked about putting the prop definitions in a separate function, and my first thought would be how to this with multiple separate instances of a Circle...
So here is a full working example with two Circle definitions, adding the props from a separate function, along with a CodePen of it here:
https://codepen.io/appurist/pen/ZEbOdeB?editors=0011
class Circle {
constructor(r) {
this._radius = r;
addProps(this);
}
}
function addProps(obj) {
Object.defineProperties(obj, {
radius: {
get: function () {
return this._radius;
},
set: function (n) {
this._radius = n;
}
},
area: {
get: function () {
return Math.PI * this._radius * this._radius;
},
set: function (n) {
this._radius = Math.sqrt(n / Math.PI);
}
}
});
}
let circle = new Circle(7);
let circle2 = new Circle(2);
console.log(`circle radius and area are: ${circle.radius} ${circle.area}`);
circle.radius = 4;
console.log(`circle radius and area now: ${circle.radius} ${circle.area}`);
circle.area = 78.53981633974483;
console.log(`circle radius and area now: ${circle.radius} ${circle.area}`);
console.log(`circle2 radius and area are: ${circle2.radius} ${circle2.area}`);
circle2.radius = 3;
console.log(`circle2 radius and area now: ${circle2.radius} ${circle2.area}`);
circle2.area = 50.26548245743669;
console.log(`circle2 radius and area now: ${circle2.radius} ${circle2.area}`);
Output of the above is:
circle radius and area are: 7 153.93804002589985
circle radius and area now: 4 50.26548245743669
circle radius and area now: 5 78.53981633974483
circle2 radius and area are: 2 12.566370614359172
circle2 radius and area now: 3 28.274333882308138
circle2 radius and area now: 4 50.26548245743669