Reputation: 958
here i have a confusion i create object called obj
obj has a key first
.
a first
key has a key second
now when i interpret below program its called function
hello()
and give output on console hello
. without accessing object property like obj.first.second
.
i didn't understand whats going on
or is there any way to prevent function call before accessing object property.
var obj ={
first:{
second:hello("hello")
}
}
//obj.first.second
function hello(url){
console.log(url)
}
any help would be appreciated
Upvotes: 0
Views: 71
Reputation:
you can also use getter with define your objects with class.
here is an example
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea() {
return this.height * this.width;
}
}
const square = new Rectangle(10, 10);
console.log(square.area);
Upvotes: 0
Reputation: 138307
Seems like you are looking for a getter, that gets executed when you access the property:
var obj = {
first: {
get second() { hello("hello") }
}
};
function hello(url) {
console.log(url);
}
// Now here hello gets called:
obj.first.second
Upvotes: 2