DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. JavaScript
  4. JavaScript’s with Statement and Why it's Deprecated

JavaScript’s with Statement and Why it's Deprecated

Axel Rauschmayer user avatar by
Axel Rauschmayer
·
Jun. 01, 11 · Interview
Like (0)
Save
Tweet
Share
3.45K Views

Join the DZone community and get the full member experience.

Join For Free
This post explains how the with statement works in JavaScript and why its use is discouraged.

Syntax and semantics

Syntax:

   with (object)
statement

introduces the properties of object as local variables in statement. Example (the braces are optional for single statements, but it is recommended to add them):

> with({ first: "John" }) { console.log("Hello "+first); }
Hello John
There is one similar case in JavaScript where the properties of an object are turned into variables and that is the global object window. All of its properties are global variables and vice versa. In contrast, variables that are declared in statement are not added to object, but to the surrounding function, where they still exist after leaving the block.
    > with({}) { var x = "abc"; }
    > x
    'abc'

The with statement is deprecated

The use of the with statement is generally discouraged. It is forbidden in strict mode:
    > function foo() { "use strict"; with({}); }
    SyntaxError: strict mode code may not contain 'with' statements
Best practice: Don’t use a with statement.
    with(foo.bar.baz) {
        console.log("Hello "+first+" "+last);
    }
Do use a temporary variable with a short name.
    var b = foo.bar.baz;
    console.log("Hello "+b.first+" "+b.last);
If you don’t want to expose the temporary variable b to the current scope, you can use an IIFE:
    (function() {
        var b = foo.bar.baz;
        console.log("Hello "+b.first+" "+b.last);
    }());
You also have the option of making the object that you want to access a parameter of the IIFE:
    (function(b) {
        console.log("Hello "+b.first+" "+b.last);
    }(foo.bar.baz));

The rationale of the deprecation

To understand why with is deprecated, look at the following example and notice how the function’s argument completely changes how it works.
    function foo(arg) {
        with(arg) {
            console.log("arg: "+arg)
        }
    }
Interaction:
    > foo("Hello");
    arg: Hello  // parameter arg
    
    > foo({});
    arg: [object Object]  // parameter arg
    
    > foo({ arg: "Hello" });
    arg: Hello  // property arg.arg
There are thus two problems that the with statement causes:
  • Performance: one cannot optimize the access to arg (or to any other variable used inside with), because one cannot predict whether arg will refer to a real variable or to a property inside the with argument. That can change with each call.
  • Security: you cannot determine what an identifier refers to by looking at its syntactic surroundings (its lexical environment). According to Brendan Eich that was the actual reason why with was deprecated, not performance considerations. Quoting a tweet of his:
    with violates lexical scope, making program analysis (e.g. for security) hard to infeasible.

From http://www.2ality.com/2011/06/with-statement.html

JavaScript

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DeveloperWeek 2023: The Enterprise Community Sharing Security Best Practices
  • The Beauty of Java Optional and Either
  • DevSecOps: The Future of Secure Software Development
  • A Deep Dive Into AIOps and MLOps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: