DEV Community

Cover image for Implementing Reactivity from scratch

Implementing Reactivity from scratch

Siddharth on August 12, 2021

Reactivity is at the heart of many web interfaces. It makes programming robust and interactive web apps much, much easier. Although most frameworks...
Collapse
 
grahamthedev profile image
GrahamTheDev

REPL isn't working but other than that this is well written and clear!

Most people haven't heard of getters and setters in JS.

Your next one should be on proxy as an alternative way to do the same / similar.

Great article! β€πŸ¦„

Collapse
 
siddharthshyniben profile image
Siddharth

Yeah, the REPL has a lot of problems. I actually opened two issues on it the last day.

Proxy is not that supported, so I chose getters/setters.

Collapse
 
intermundos profile image
intermundos

What do you mean proxy not supported? Proxy is much supported. In fact, vue 3 reactivity is base on Proxy.

Thread Thread
 
siddharthshyniben profile image
Siddharth

It's not supported everywhere

Thread Thread
 
intermundos profile image
intermundos

Yes, for sure. Only if you support some esoteric platforms keep writing getters and setters.

Collapse
 
grahamthedev profile image
GrahamTheDev

Proxy is not that supported, so I chose getters/setters

But you are using arrow functions so the difference will be next to nothing!

REPL seems to work now!

Thread Thread
 
miketalbot profile image
Mike Talbot ⭐

TBF arrow functions can be provided by babel, proxy cannot be polyfilled. I don't use them either because of the need for IE11 support.

Collapse
 
merichard123 profile image
Richard • Edited

This is really well written! Thank you for the explanation!!

Just checking:

set name (val) {
        this.name = name;
        // TODO notify
 }

Should this have been this.name = val taking the value from the setter param?

Collapse
 
siddharthshyniben profile image
Siddharth

Welcome 😁

Collapse
 
toraritte profile image
Attila Gulyas

I got bogged down at the very beginning at const who =, especially when I read "Later, you change who"... I was under the impression that JS constants cannot be changed, and it also didn't work for me when I tried, but I may be missing something basic.

Collapse
 
siddharthshyniben profile image
Siddharth

That's my bad, constants can't be changed. I'll fix it. Thanks!

Collapse
 
johnwarner profile image
John Warner

Nice article. The Modstache library I wrote uses this approach for reactivity. It uses Object.defineProperty to modify object property getter and setter functions to automatically modify the DOM when the property is modified. It also uses a Proxy for array manipulations. It's an efficient way to detect changes to an object and perform an action, log activity, etc.

Collapse
 
zyabxwcd profile image
Akash

noice

Collapse
 
souksyp profile image
Souk Syp.

Cool! Look similar to Observer pattern

Collapse
 
tojacob profile image
Jacob Samuel G.

It's beautiful. I had never thought of reactivity in this way. Thanks.

Collapse
 
alisher profile image
Alisher-Usmonov

I think that codes needs some improvement for wroking with primitive values