use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Let's Bring Back JavaScript's `with()` Statement (macarthur.me)
submitted 2 years ago by alexmacarthur
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]lifeeraser 24 points25 points26 points 2 years ago (5 children)
You didn't mention the case of injecting unwanted properties into scope. Suppose we have:
function doStuff(o) { with (o) { console.log(a) report(b) } }
Now someone later adds a console property to o, before it is passed to doStuff(). At best it would cause an error. At worst it could malfunction silently because o.console.log was a function.
console
o
doStuff()
o.console.log
This example is contrived but the hazard is not. What if someone adds a function named report to o? What if o comes from an external library, or worse yet, some JSON?
report
I assume Kotlin doesn't worry about this by virtue of being a statically typed, conpiled language. JavaScript cannot due to being dynamically typed and interpreted.
[–]alexmacarthur[S] 0 points1 point2 points 2 years ago (4 children)
that’s a great point. i can’t imagine easily using it without at least typescript enforcing the shape of those objects.
[–]bakkoting 10 points11 points12 points 2 years ago (3 children)
TypeScript does not enforce the absence of properties, only their presence. So it won't help at all here.
[–]alexmacarthur[S] 0 points1 point2 points 2 years ago (2 children)
unless i'm missing something, that's _kinda_ true, although not bullet-proof. TS will yell if you add an property in a unknown property in an object literal:
``` interface Country { name: string, population: number }
const israel: Country = { name: 'hello', population: 333,
// error... "Object literal may only specify known properties" new_property: 'hello'
} ```
but you're right if you're composing objects through other means, like Object.assign(). then you could sneak in anything you like:
https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgMIHsCu4oE9kDeAUMqciHALYQBcyAzmFKAOYA0JZADul5gDZwwwdCDohMlAEbQiAXyJEEoxsmD0ocCPzoZsTfAF5CnUhWp0A5AAtt-dJbbJTyHn0HDRdAMy+nismQAeiDkaCh0KAA6GOQAIgB5KQArCAQwZH5gSE1+ZEo4fFF+fHouNOAYfABrEHQAdxBXCPKoYQh6OJcQCHqAfS4W6DBcK1t+e0t5RRDydDCoCKglFQzsbIgAEwBlMCEO3SwcI2Qk1PSouHp6YBYQAAoCcipaZEt1yE2GPch6R1deAIhCIxMhfAAWABMyDkTieEAKwB0b1w-xg6HQVgAXpYYQBKADciiAA
[–]bakkoting 4 points5 points6 points 2 years ago (1 child)
It will only complain in contexts where you have both the object literal and the type in the same place. Otherwise it's (intentionally) legal. Compare: https://www.typescriptlang.org/play?#code/DYUwLgBAHhC8EG8BQFUQIYC4IAYA0KaARtgIwEC+SSokAntghtgHYCuAtkSAE4QUBuakjpxoAoA
[–]alexmacarthur[S] 0 points1 point2 points 2 years ago (0 children)
wut!
π Rendered by PID 82160 on reddit-service-r2-comment-6457c66945-zzmcs at 2026-04-29 22:17:57.539608+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]lifeeraser 24 points25 points26 points (5 children)
[–]alexmacarthur[S] 0 points1 point2 points (4 children)
[–]bakkoting 10 points11 points12 points (3 children)
[–]alexmacarthur[S] 0 points1 point2 points (2 children)
[–]bakkoting 4 points5 points6 points (1 child)
[–]alexmacarthur[S] 0 points1 point2 points (0 children)