with Statement

with (obj) { ... } temporarily treats the object's properties as if they were in the local scope.
Its use is strongly discouraged because it makes code ambiguous, slower, and it is disallowed in strict mode and ES modules.


Syntax

with (object) {
  // statements (sloppy mode only; not allowed in strict/modules)
}

Examples

// ১) বেসিক — অবজেক্টের প্রপার্টি লোকাল স্কোপে
const ব্যবহারকারী = { নাম: "Mamun", শহর: "Dhaka" };

সাথে (ব্যবহারকারী) {
  কনসোল.লগ(নাম, শহর);   // ব্যবহারকারী.নাম, ব্যবহারকারী.শহর
}

// ২) অস্পষ্টতা — লিখলে কী হবে?
const প্রোফাইল = { নাম: "Rafi" };
সাথে (প্রোফাইল) {
  নাম = "Zara";          // যদি 'নাম' থাকে → প্রোফাইল.নাম বদলাবে
                         // না থাকলে sloppy‑mode‑এ global তৈরি হবে! (খুব খারাপ)
}

// ৩) strict mode‑এ নিষিদ্ধ (SyntaxError)
"use strict";
// সাথে ({ a: 1 }) { কনসোল.লগ(a); } // ❌ SyntaxError (module‑এও নিষিদ্ধ)

// ৪) বিকল্প — ডেস্ট্রাকচারিং/লোকাল উপনাম
const order = { id: 10, total: 999, customer: { name: "Alice" } };
const { id, total, customer: { name } } = order;
কনসোল.লগ(id, total, name);

// অথবা অস্থায়ী উপনাম
const o = order.customer;
কনসোল.লগ(o.name);

Notes

  • Avoid with. It hampers optimization and makes name resolution ambiguous (hard for humans and engines).
  • Not allowed in strict mode or ES modules (modules are always strict).
  • Assignments inside with are risky: may write to the object or accidentally create/modify a global in sloppy mode.
  • Prefer destructuring, local aliases, or direct qualified access (obj.prop).
  • Keep one language style (English/Bangla/Banglish) per file.