TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Advanced objects in JavaScript

162 点作者 btipling超过 10 年前

8 条评论

yoran超过 10 年前
I once participated in a web security research project where we used ES6 Proxies to sandbox third-party scripts, basically controlling what DOM operations they're allowed to do in a whitelist. This is relevant when you include sketchy advertisement scripts or scripts you don't trust (e.g. "trusted" scripts but delivered through an untrusted CDN). By default, no operations were allowed and by whitelisting until there were no more errors, we managed to effectively completely sandbox jQuery, Google Maps and Google Analytics. None of these scripts were allowed to execute more operations than the ones exactly required for them to function. Proxies were an elegant way to intercept getters and setters on window, document, location, etc...
评论 #8220159 未加载
评论 #8221197 未加载
评论 #8221912 未加载
评论 #8224599 未加载
评论 #8219848 未加载
bshimmin超过 10 年前
Articles like this, about up-and-coming features in JavaScript, usually make me think two things:<p>1) JavaScript is starting to become a &quot;big&quot; language, in terms of number of keywords and syntax;<p>2) While many of these things that JavaScript is (finally) gaining are things we&#x27;re familiar with in other languages (and have empirically found to be useful), they often seem somewhat more unwieldy, fiddly, and complicated in JavaScript.<p>As a handwavy example, Ruby generally has most of the stuff these sorts of articles describe, and it can be complicated at times, but usually (in my experience) only when you&#x27;re trying to do something complicated anyway; in JavaScript, a lot of it looks quite complicated out of the box even when you&#x27;re doing something that ought to be simple.<p>There are many JavaScript developers out there, some with years of experience, who are confused about, say, `var Foo = function() {}` versus `function Foo() {}`, or the difference between `new Foo()` and `Object.create(Foo.prototype)`. To be honest, it slightly worries me that even more complexity is being added at this point.
评论 #8222197 未加载
VeejayRampay超过 10 年前
Good thing that this is all coming to Javascript. But I can&#x27;t help but think we&#x27;re in bizarro land when the main language of the most ubiquitous medium of the 21st century is only getting all that rather basic stuff (at least by all the other languages&#x27; standards) in 2015 or something...
评论 #8220095 未加载
评论 #8219751 未加载
jvickers超过 10 年前
Alternatives to getType and setType functions (while still not using getters and setters):<p>Have a single polymorphic function called type: type() gets the type, type(value) sets the type.<p>Have .get and .set functions, called like: get(&#x27;type&#x27;), set(&#x27;type&#x27;, value).
评论 #8219467 未加载
tomphoolery超过 10 年前
This is pretty cool, but something about the notation confused me. Say we have an object called &quot;foo&quot;:<p><pre><code> var foo = {}; </code></pre> Is it really necessary to define properties on it with this notation?<p><pre><code> Object.defineProperty(foo, &#x27;bar&#x27;, { value: &#x27;baz&#x27; }); </code></pre> Since foo itself is an Object, wouldn&#x27;t it also have this method? Why can&#x27;t I define a property on foo like this?<p><pre><code> foo.defineProperty(&#x27;bar&#x27;, { value: &#x27;baz&#x27; });</code></pre>
评论 #8220642 未加载
Excavator超过 10 年前
Small correction: Symbol¹ is available in Firefox since back in ~June and should arrive in Firefox Beta in ~10days.<p>1: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a>
评论 #8219942 未加载
评论 #8220294 未加载
wging超过 10 年前
&gt; You can use Object.getOwnPropertyNames to get all the properties defined on an object. This might be a nice alternative to a for in loop.<p>Careful. These have different behavior for own properties which are not enumerable. A for-in loop doesn&#x27;t include them, but Object.getOwnPropertyNames does.
评论 #8221203 未加载
评论 #8222214 未加载
elwell超过 10 年前
CoffeeScript abstracts out a lot of of awkwardness of working with JS objects.
评论 #8219862 未加载
评论 #8222130 未加载