Get different object property value in each access | JavaScript Interview Question - 49

preview_player
Показать описание
JavaScript Interview Question - 49 | In this video, we will see how to solve a medium-difficulty problem asked in frontend engineer interview to SDE1, SDE2, and SDE3.

We have to implement a solution in which we get different property values of an object every time it is accessed. This question was asked in Rippiling's interview for SDE2+.

Loved the question? I have 120+ such solved problems in my ebook "JavaScript Interview Guide". If you are preparing for Interviews and looking for a solutions book, Get my Ebook.

Social links
Рекомендации по теме
Комментарии
Автор

Hi Prashant, solved it on my own.

//Below is my code
let obj = {
a: 0
}

const handler = {
get: (target, prop) => {
if(prop === 'a'){
target[prop] += 1
}
return target[prop]
}
}

obj = new Proxy(obj, handler)

console.log(obj.a)
console.log(obj.a)

obj.a = 10
console.log(obj.a)
console.log(obj.a)
console.log(obj.a)

akash-kumar
Автор

Learnt something new, thanks for such good content

rahuljain
Автор

Its better to use debugger when an object is modifying just after definition instead of relying on console.

sumitbhardwaz
Автор

Learnt something new. Thank you for this :)

anandbaraik
visit shbcf.ru