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 的简介了。

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

194 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. Hmm is anyone else encountering problems with the images on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any feed-back would be greatly appreciated.|

  6. 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!|

  7. 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!|

  8. 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!|

  9. 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!|

  10. Everyone loves what you guys are usually up too. Such clever work and exposure! Keep up the terrific works guys I’ve incorporated you guys to my personal blogroll.|

  11. 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!|

  12. 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?|

  13. 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.|

  14. 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?|

  15. 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!|

  16. 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.|

  17. 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.|

  18. 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.|

  19. 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!|

  20. 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.|

  21. 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!|

  22. 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.|

  23. 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.|

  24. 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.|

  25. 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?|

  26. 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!|

  27. 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!|

  28. 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!|

  29. 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.|

  30. Hi Dear, are you in fact visiting this web page on a regular basis, if so afterward you will definitely take nice experience.|

  31. 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.|

  32. 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.|

  33. Ahaa, its nice discussion regarding this piece of writing at this place at this blog, I have read all that, so now me also commenting at this place.|

  34. 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.|

  35. 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|

  36. 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!|

  37. 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!|

  38. 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!|

  39. 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!|

  40. 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.|

  41. I for all time emailed this blog post page to all my friends, since if like to read it afterward my links will too.|

  42. 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!!|

  43. 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!|

  44. I visited multiple websites however the audio quality for audio songs present at this web site is truly marvelous.|

  45. 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.|

  46. 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.|

  47. 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!|

  48. 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.|

  49. 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|

  50. 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|

  51. I like what you guys are up too. This sort of clever work and coverage! Keep up the good works guys I’ve incorporated you guys to our blogroll.|

  52. 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.|

  53. 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!|

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

  55. 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.|

  56. 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.|

  57. Post writing is also a excitement, if you know afterward you can write otherwise it is complex to write.|

  58. 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!|

  59. 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.|

  60. 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.

  61. 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.|

  62. 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!|

  63. 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?|

  64. You can certainly see your skills within the work you write. The sector hopes for more passionate writers like you who are not afraid to say how they believe. At all times follow your heart.|

  65. I really like what you guys are usually up too. This type of clever work and reporting! Keep up the great works guys I’ve incorporated you guys to my personal blogroll.|

  66. Hi there would you mind sharing which blog platform you’re working with? I’m looking to start my own blog in the near future but I’m having a tough time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I’m looking for something completely unique. P.S Apologies for being off-topic but I had to ask!|

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

  68. Hey there! Someone in my Myspace group shared this website with us so I came to look it over. I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers! Terrific blog and brilliant design.|

  69. Please let me know if you’re looking for a writer for your site. You have some really great posts and I think I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some content for your blog in exchange for a link back to mine. Please shoot me an e-mail if interested. Many thanks!|

  70. If you are going for best contents like I do, just go to see this web page daily because it gives feature contents, thanks|

  71. I wanted to draft you a tiny remark to help thank you very much over again just for the breathtaking knowledge you have featured here. This is really unbelievably open-handed with people like you to offer without restraint all a lot of people would have offered for sale for an e book to make some dough for themselves, particularly now that you might have done it in case you considered necessary. These inspiring ideas in addition served to be a easy way to understand that other people have a similar zeal similar to my very own to find out somewhat more on the topic of this condition. I’m sure there are thousands of more enjoyable sessions ahead for those who see your blog post.

  72. Hi my family member! I want to say that this article is amazing, great written and come with approximately all important infos. I’d like to look extra posts like this .|

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

  74. Hola! I’ve been following your blog for some time now and finally got the courage to go ahead and give you a shout out from Huffman Texas! Just wanted to say keep up the excellent work!|

  75. Generally I don’t read post on blogs, but I would like to say that this write-up very compelled me to try and do it! Your writing style has been surprised me. Thank you, quite great article.|

  76. I want to express my thanks to the writer just for rescuing me from this particular dilemma. After looking out through the world wide web and seeing recommendations which were not productive, I figured my life was done. Existing minus the approaches to the issues you’ve fixed by way of this review is a critical case, and the kind which might have in a negative way affected my entire career if I had not noticed your website. That expertise and kindness in touching all the things was very helpful. I’m not sure what I would’ve done if I hadn’t come upon such a solution like this. I can also now relish my future. Thanks so much for the specialized and amazing help. I won’t hesitate to refer your blog to anybody who would need direction on this subject matter.

  77. I’ve been browsing on-line more than three hours as of late, but I never discovered any fascinating article like yours. It is beautiful value sufficient for me. Personally, if all website owners and bloggers made just right content as you probably did, the internet will be much more useful than ever before.|

  78. I’ve been surfing online greater than three hours these days, yet I by no means found any interesting article like yours. It is beautiful price enough for me. In my opinion, if all web owners and bloggers made excellent content as you did, the internet will likely be much more useful than ever before.|

  79. I intended to write you the very little word to help say thank you the moment again on the amazing basics you’ve shown above. It was certainly remarkably open-handed with people like you to deliver easily precisely what most of us could have offered as an e book to earn some profit for their own end, mostly since you could have done it if you decided. Those principles also acted to provide a great way to realize that other individuals have the identical zeal just like my own to know the truth way more on the subject of this issue. I’m certain there are thousands of more enjoyable situations in the future for those who scan through your website.

  80. Thank you for every other informative site. Where else may just I am getting that type of information written in such an ideal method? I have a challenge that I am simply now operating on, and I have been at the glance out for such info.|

  81. Aw, this was a really good post. Taking the time and actual effort to create a very good article… but what can I say… I put things off a whole lot and never manage to get anything done.|

  82. Woah! I’m really enjoying the template/theme of this site. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between user friendliness and appearance. I must say you’ve done a awesome job with this. Also, the blog loads super quick for me on Opera. Outstanding Blog!|

  83. Does your blog have a contact page? I’m having a tough time locating it but, I’d like to send you an e-mail. 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.|

  84. I think this is among the most important info for me. And i am glad reading your article. But should remark on few general things, The website style is perfect, the articles is really excellent : D. Good job, cheers|

  85. Hello would you mind sharing which blog platform you’re using? I’m looking to start my own blog in the near future but I’m having a tough time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I’m looking for something completely unique. P.S My apologies for getting off-topic but I had to ask!|

  86. I needed to put you a little word in order to thank you very much as before over the remarkable tricks you have discussed in this article. This is particularly generous of you to deliver unreservedly precisely what many individuals could have offered as an ebook to get some dough for themselves, specifically considering that you could possibly have done it in the event you decided. These concepts also acted as the fantastic way to be certain that most people have similar eagerness really like mine to know great deal more in terms of this matter. I think there are lots of more enjoyable sessions in the future for individuals that examine your website.

  87. I’ve read a few excellent stuff here. Definitely worth bookmarking for revisiting. I wonder how so much attempt you put to make one of these fantastic informative web site.|

  88. Link exchange is nothing else however it is just placing the other person’s web site link on your page at appropriate place and other person will also do similar for you.|

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

  90. hey there and thank you for your information – I have definitely picked up something new from right here. I did however expertise several technical points using this site, since I experienced to reload the web site a lot of times previous to I could get it to load properly. I had been wondering if your hosting is OK? Not that I’m complaining, but sluggish loading instances times will sometimes affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords. Well I’m adding this RSS to my e-mail and can look out for a lot more of your respective interesting content. Make sure you update this again very soon.|

  91. I have to express appreciation to the writer for bailing me out of this type of dilemma. Just after browsing through the world-wide-web and seeing proposals which are not powerful, I was thinking my entire life was gone. Being alive minus the answers to the problems you have resolved through your guide is a crucial case, as well as the ones which could have badly damaged my entire career if I had not encountered the website. The training and kindness in dealing with all areas was very useful. I don’t know what I would’ve done if I had not encountered such a subject like this. I’m able to at this point look ahead to my future. Thanks for your time so much for the skilled and sensible guide. I won’t hesitate to propose the sites to anybody who should have guidance about this situation.

  92. Hi would you mind letting me know which web host you’re working with? I’ve loaded your blog in 3 completely different browsers and I must say this blog loads a lot quicker then most. Can you recommend a good internet hosting provider at a fair price? Cheers, I appreciate it!|

  93. Your style is so unique compared to other people I have read stuff from. I appreciate you for posting when you’ve got the opportunity, Guess I’ll just bookmark this page.|

  94. Hi, i read your blog occasionally and i own a similar one and i was just curious if you get a lot of spam feedback? If so how do you prevent it, any plugin or anything you can advise? I get so much lately it’s driving me crazy so any support is very much appreciated.|

  95. Hello! Someone in my Myspace group shared this site with us so I came to check it out. I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers! Outstanding blog and great style and design.|

  96. Heya i’m for the primary time here. I found this board and I to find It truly helpful & it helped me out a lot. I’m hoping to give one thing again and help others like you aided me.|

  97. Your method of telling all in this article is truly good, every one can without difficulty understand it, Thanks a lot.|

  98. What’s Taking place i am new to this, I stumbled upon this I have found It absolutely useful and it has helped me out loads. I’m hoping to contribute & aid other users like its helped me. Great job.|

  99. My brother suggested I might like this web site. He was entirely right. This post truly made my day. You cann’t imagine simply how much time I had spent for this info! Thanks!|

  100. Having read this I thought it was extremely enlightening. I appreciate you spending some time and energy to put this information together. I once again find myself personally spending a lot of time both reading and posting comments. But so what, it was still worth it

  101. Thank you for some other excellent post. The place else may just anybody get that type of info in such an ideal means of writing? I have a presentation next week, and I’m at the search for such information.|

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

  103. Hello there, I found your website by the use of Google at the same time as searching for a similar subject, your website got here up, it appears to be like good. I’ve bookmarked it in my google bookmarks.

  104. Hi, I do believe this is a great web site. I stumbledupon it 😉 I will come back yet again since i have saved as a favorite it. Money and freedom is the best way to change, may you be rich and continue to guide others.

发表评论

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