Proxy 对象是做什么用的?

打开 Chrome 控制台,输入 window.Proxy ,你会发现 JavaScript 已经内置了一个全局的 Proxy 对象,请问这个对象是做什么用的?

其实你用关键词「Proxy MDN」搜索一下,就能得到一个详细的教程。(在关键词后面加 MDN 是一个前端必备的小技巧哦)

我今天只做一个简单的介绍。

假设我们有一个数据(对象)data,内容为

var data = {
  username: 'Frank',
  age: 26
}

现在我们给 data 创建一个代理 proxy

var proxy = new Proxy(data, {set: function(){...}, get: function(){...} })

那么,「proxy 就全权代理 data 了」,这话是什么意思呢?

意思就是 data 放假去了,如果你有任何事情要找 data,直接找 proxy 就好了,proxy 现在是 data 的秘书、代理人。

比如原本你如果要改 username,那么应该写 data.username = ‘frank’;

那么现在你只需要写 proxy.username = ‘frank’ 就好了。

原本你如果想写 console.log(data.username),现在也只需要 console.log(proxy.username) 就可以了。

这样做什么意义?

意义就是你能监控每一次对 data 的读写操作。

proxy.username = ‘frank’ 这句话实际上会运行 set 方法。set 方法可以对传入的值进行监控和过滤。

假设 PM 要求「username 前后不能含有空格」,用 Proxy 就很好实现这一需求,只需要把 set 写成这样:

set: function(obj, prop, value){
  obj[prop] = value.trim()
}

再假设 PM 要求「统计 username 被读取的次数」,那么我们只需要把 get 写成这样:

get: function(obj, prop){
  if(prop === 'username'){
    count += 1
  }
  return obj[prop]
}

双向绑定

既然用 Proxy 能监控一个变量的读写情况,那么我们就很容易实现一个双向绑定了。

具体代码看这里

以上,就是 Proxy 的简介了。

作者:方应杭,想学前端?进群

105 thoughts on “Proxy 对象是做什么用的?”

  1. Have you ever considered creating an ebook or guest authoring on other sites? I have a blog based on the same topics you discuss and would love to have you share some stories/information. I know my readers would appreciate your work. If you’re even remotely interested, feel free to shoot me an e-mail.|

  2. Pretty nice post. I just stumbled upon your weblog and wished to say that I have truly enjoyed surfing around your blog posts. After all I will be subscribing to your feed and I hope you write again soon!|

  3. Hey there! I could have sworn I’ve been to this blog before but after checking through some of the post I realized it’s new to me. Nonetheless, I’m definitely delighted I found it and I’ll be book-marking and checking back often!|

  4. You’re so awesome! I don’t suppose I’ve read a single thing like this before. So great to discover someone with some unique thoughts on this subject matter. Seriously.. thanks for starting this up. This website is one thing that is needed on the web, someone with some originality!|

  5. Wonderful site you have here but I was curious if you knew of any community forums that cover the same topics discussed here? I’d really like to be a part of community where I can get feedback from other experienced people that share the same interest. If you have any recommendations, please let me know. Cheers!|

  6. Someone essentially assist to make critically posts I would state. This is the first time I frequented your website page and thus far? I amazed with the analysis you made to make this particular publish amazing. Excellent job!|

  7. It’s the best time to make some plans for the future and it’s time to be happy. I have read this post and if I could I want to suggest you few interesting things or advice. Maybe you could write next articles referring to this article. I desire to read more things about it!|

  8. Hi there this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding skills so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!|

  9. Hello! I realize this is kind of off-topic but I needed to ask. Does operating a well-established website such as yours require a lot of work? I am completely new to blogging but I do write in my diary daily. I’d like to start a blog so I can easily share my own experience and feelings online. Please let me know if you have any suggestions or tips for new aspiring blog owners. Appreciate it!|

  10. Currently it seems like Expression Engine is the top blogging platform available right now. (from what I’ve read) Is that what you are using on your blog?|

  11. Does your blog have a contact page? I’m having a tough time locating it but, I’d like to shoot you an e-mail. I’ve got some recommendations for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it expand over time.|

  12. I’m really loving the theme/design of your website. Do you ever run into any browser compatibility problems? A handful of my blog readers have complained about my site not operating correctly in Explorer but looks great in Safari. Do you have any suggestions to help fix this problem?|

  13. great submit, very informative. I wonder why the opposite specialists of this sector don’t realize this. You must continue your writing. I’m sure, you’ve a great readers’ base already!|

  14. I’ve been surfing online more than 3 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my opinion, if all website owners and bloggers made good content as you did, the net will be much more useful than ever before.|

  15. Hi, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam comments? If so how do you stop it, any plugin or anything you can recommend? I get so much lately it’s driving me crazy so any assistance is very much appreciated.|

  16. It’s amazing to pay a quick visit this web site and reading the views of all colleagues on the topic of this piece of writing, while I am also eager of getting familiarity.|

  17. Wow that was odd. I just wrote an extremely long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Anyways, just wanted to say wonderful blog!|

  18. Howdy just wanted to give you a brief heads up and let you know a few of the pictures aren’t loading properly. I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same outcome.|

  19. Hey there would you mind letting me know which hosting company you’re utilizing? I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads a lot quicker then most. Can you suggest a good hosting provider at a fair price? Many thanks, I appreciate it!|

  20. I blog frequently and I genuinely thank you for your information. The article has truly peaked my interest. I’m going to bookmark your blog and keep checking for new details about once per week. I opted in for your Feed as well.|

  21. Hello there I am so happy I found your blog page, I really found you by error, while I was browsing on Askjeeve for something else, Anyhow I am here now and would just like to say thanks for a marvelous post and a all round interesting blog (I also love the theme/design), I don’t have time to go through it all at the moment but I have bookmarked it and also added your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the great work.|

  22. Hi everyone, it’s my first pay a visit at this web page, and paragraph is in fact fruitful in favor of me, keep up posting these posts.|

  23. Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how could we communicate?|

  24. We absolutely love your blog and find most of your post’s to be just what I’m looking for. Does one offer guest writers to write content for yourself? I wouldn’t mind producing a post or elaborating on a few of the subjects you write in relation to here. Again, awesome blog!|

  25. Howdy! I understand this is sort of off-topic however I had to ask. Does managing a well-established website such as yours require a large amount of work? I’m completely new to running a blog but I do write in my diary every day. I’d like to start a blog so I can easily share my experience and feelings online. Please let me know if you have any recommendations or tips for new aspiring blog owners. Appreciate it!|

  26. Very great post. I simply stumbled upon your blog and wished to say that I’ve really loved surfing around your weblog posts. In any case I will be subscribing for your rss feed and I’m hoping you write again very soon!|

  27. I’m impressed, I have to admit. Rarely do I come across a blog that’s both equally educative and interesting, and without a doubt, you’ve hit the nail on the head. The issue is an issue that not enough folks are speaking intelligently about. I’m very happy that I stumbled across this in my search for something concerning this.|

  28. Simply want to say your article is as astounding. The clarity in your post is simply nice and i can assume you’re an expert on this subject. Well with your permission let me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please carry on the rewarding work.|

  29. Does your blog have a contact page? I’m having trouble locating it but, I’d like to send you an email. I’ve got some recommendations for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it develop over time.|

  30. I have been browsing on-line more than three hours today, yet I by no means discovered any attention-grabbing article like yours. It’s lovely value sufficient for me. In my opinion, if all site owners and bloggers made just right content material as you did, the web can be a lot more useful than ever before.|

  31. I don’t know if it’s just me or if perhaps everyone else experiencing problems with your website. It appears as though some of the written text in your content are running off the screen. Can somebody else please provide feedback and let me know if this is happening to them as well? This might be a problem with my internet browser because I’ve had this happen previously. Thank you|

  32. Greetings from Ohio! I’m bored to death at work so I decided to check out your blog on my iphone during lunch break. I really like the info you provide here and can’t wait to take a look when I get home. I’m shocked at how quick your blog loaded on my cell phone .. I’m not even using WIFI, just 3G .. Anyways, wonderful blog!|

  33. My partner and I absolutely love your blog and find nearly all of your post’s to be what precisely I’m looking for. can you offer guest writers to write content to suit your needs? I wouldn’t mind publishing a post or elaborating on most of the subjects you write concerning here. Again, awesome website!|

  34. It is the best time to make some plans for the future and it’s time to be happy. I have read this post and if I could I desire to suggest you few interesting things or suggestions. Perhaps you could write next articles referring to this article. I wish to read more things about it!|

  35. Howdy would you mind letting me know which webhost you’re utilizing? I’ve loaded your blog in 3 different browsers and I must say this blog loads a lot faster then most. Can you recommend a good web hosting provider at a reasonable price? Thanks a lot, I appreciate it!|

  36. My partner and I stumbled over here different page and thought I may as well check things out. I like what I see so now i am following you. Look forward to finding out about your web page for a second time.|

  37. Oh my goodness! Amazing article dude! Many thanks, However I am experiencing issues with your RSS. I don’t understand why I cannot join it. Is there anybody getting similar RSS problems? Anybody who knows the solution can you kindly respond? Thanks!!|

  38. Very great post. I just stumbled upon your weblog and wished to mention that I’ve really loved browsing your weblog posts. In any case I’ll be subscribing in your feed and I am hoping you write again soon!|

  39. I have been browsing online greater than three hours these days, but I by no means discovered any fascinating article like yours. It’s beautiful value sufficient for me. In my opinion, if all web owners and bloggers made excellent content as you did, the internet can be a lot more helpful than ever before.|

  40. We stumbled over here from a different page and thought I might as well check things out. I like what I see so i am just following you. Look forward to looking at your web page yet again.|

  41. Woah! I’m really loving the template/theme of this website. It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between usability and visual appeal. I must say you have done a fantastic job with this. In addition, the blog loads super fast for me on Chrome. Excellent Blog!|

  42. I’ve been browsing online more than 4 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. Personally, if all web owners and bloggers made good content as you did, the net will be much more useful than ever before.|

  43. Thank you a lot for sharing this with all people you actually recognise what you are speaking approximately! Bookmarked. Kindly also seek advice from my site =). We can have a link trade agreement between us|

  44. I know this if off topic but I’m looking into starting my own weblog and was curious what all is required to get set up? I’m assuming having a blog like yours would cost a pretty penny? I’m not very internet savvy so I’m not 100 certain. Any tips or advice would be greatly appreciated. Thank you|

  45. Aw, this was an exceptionally good post. Taking the time and actual effort to make a really good article… but what can I say… I procrastinate a whole lot and never seem to get nearly anything done.|

  46. Whats up this is kinda of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding experience so I wanted to get advice from someone with experience. Any help would be enormously appreciated!|

  47. Asking questions are truly good thing if you are not understanding anything entirely, but this piece of writing provides fastidious understanding even.|

  48. Its such as you read my thoughts! You appear to know so much about this, such as you wrote the e-book in it or something. I feel that you just could do with a few to drive the message home a bit, however other than that, that is magnificent blog. A fantastic read. I will certainly be back.|

  49. Hi, I do believe this is a great website. I stumbledupon it 😉 I may come back once again since i have book-marked it. Money and freedom is the greatest way to change, may you be rich and continue to guide others.|

  50. What i don’t understood is in reality how you are no longer really much more neatly-preferred than you might be right now. You are very intelligent. You understand thus considerably in the case of this topic, made me for my part consider it from so many various angles. Its like women and men don’t seem to be interested except it is one thing to accomplish with Girl gaga! Your own stuffs nice. Always maintain it up!|

  51. I’ve been surfing online greater than three hours nowadays, yet I never found any fascinating article like yours. It is pretty price sufficient for me. Personally, if all site owners and bloggers made excellent content as you did, the net will be a lot more useful than ever before.|

  52. Hello! I just now would choose to give a huge thumbs up for the fantastic info you might have here with this post. I will be returning to your site for more soon.

  53. Hey There. I found your blog using msn. This is a really well written article. I will make sure to bookmark it and return to read more of your useful information. Thanks for the post. I will definitely return.|

  54. Hello, I think your website might be having browser compatibility issues. When I look at your website in Chrome, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, great blog!|

  55. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what youre talking about, why waste your intelligence on just posting videos to your site when you could be giving us something informative to read?|

发表评论

电子邮件地址不会被公开。