JavaScript and Proxy

Joined
Feb 12, 2020
Messages
8
Reaction score
0
I can't seem to find how to best use proxy in javascript to watch objects for changes. An example is how do i delete a proxy after i am done using it? is it as simple as using "delete"?
 
Joined
Nov 27, 2019
Messages
163
Reaction score
28
I don't know what your talking about with the word PROXY. If you want JS to watch an object for changes I would use a listener
document.getElementById("myDIV").addEventListener("mousemove", myFunction);

and remove it with
document.getElementById("myDIV").removeEventListener("mousemove", myFunction);
 
Joined
Feb 12, 2020
Messages
8
Reaction score
0
I don't know what your talking about with the word PROXY. If you want JS to watch an object for changes I would use a listener
document.getElementById("myDIV").addEventListener("mousemove", myFunction);

and remove it with
document.getElementById("myDIV").removeEventListener("mousemove", myFunction);


Proxy is used to listen to javascript objects for change. I'm not merely listening to elements for change.

JavaScript:
var object = {a:1,b,2};

var proxy = new Proxy(object,{
     get:function(obj,prop){
     },
     set:function(obj, prop, value){
     }
});

This basically creates an object that executes the get and set function, so if i were to add another value inside the object using this i would say proxy['c']=3 and it would execute the "set" part of the proxy and return the object with the original values, property which is the Object key and the new value.

Get will get executed when I retrieve a value from it.

This is modern JavaScript.
 
Joined
Nov 27, 2019
Messages
163
Reaction score
28
pdiddles I am asking you for help now. Back in 2015 I studied everything new in ES6. I came away with the notion they were trying to become C and that there was a lot of BS in the new spec. "let", for example. "proxy" must have been labeled - worthless in my mind and until now have never seen it in any program.

I have reread a lot of articles on what it does and how, but still have no reason where this could be used nor why it should be used. So that is my question to you. Enlighten me please. Give me some examples.
 
Joined
Feb 12, 2020
Messages
8
Reaction score
0
pdiddles I am asking you for help now. Back in 2015 I studied everything new in ES6. I came away with the notion they were trying to become C and that there was a lot of BS in the new spec. "let", for example. "proxy" must have been labeled - worthless in my mind and until now have never seen it in any program.

I have reread a lot of articles on what it does and how, but still have no reason where this could be used nor why it should be used. So that is my question to you. Enlighten me please. Give me some examples.

A use case for proxy is something like this: Lets say a user is logged in and their information is stored in a JSON object, you then from that point create a proxy like described above. They go to the account page and change their first name. Because of the change, it triggers the proxy set method, and you from that point can make an ajax call to the server that the information was updated. It's more to make your code look prettier. ES6 was mostly just syntactic sugar to have more readable code .

If you have used angularjs 1.x at any point, you may have used their $scope.$watch method which allows you to watch objects for changes, It is similar to proxy except proxy does not need a bloated framework to achieve it.

Let was about variable scoping. the "window" object holds all of the variables inside of it, so when you create a function or any variable or object, it gets put into the window object. Using let does not do that, that is why it's good for scoping.

Consider this code:
JavaScript:
for(var i = 0; i < 5; i++){

}
console.log(i)

The log would then spit out 5 even though it is outside of the for loop, using this code:

JavaScript:
for(let i = 0; i < 5; i++){

}
console.log(i)

would output undefined.
 
Joined
Nov 27, 2019
Messages
163
Reaction score
28
I mean things like proxy isn't my cuppa. Can think of other ways of doing this with JS. Nice thing about the language is you have a choice on how you do things. ES6 gave us some nice things and some that are not so nice; nor are they upgrades.
 
Joined
Feb 12, 2020
Messages
8
Reaction score
0
I mean things like proxy isn't my cuppa. Can think of other ways of doing this with JS. Nice thing about the language is you have a choice on how you do things. ES6 gave us some nice things and some that are not so nice; nor are they upgrades.

The updates they gave us are all logical and offer us a way that makes more sense. Yes we could program classes before, but now we have actual class syntax. Promises are fantastic. I will concede on the name "Proxy". The old deprecated way "object.observe" made a lot more sense. It's like taking the method "object.keys" and replacing it with "keeymakergenerator". Doesn't make a lot of sense why this was done.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top