> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript New

The `new` operator creates an instance of a user-defined object type or of one of the built-in object types that possesses a constructor function. It fundamentally alters the execution context of a function call, shifting it from a standard invocation to a constructor invocation.

```javascript theme={"dark"}
new constructor
new constructor()
new constructor(arg1, arg2, /* …, */ argN)
```

When a function is invoked with the `new` keyword, the JavaScript engine executes a strict four-step internal sequence:

1. **Object Creation:** A new, blank JavaScript object is allocated in memory.
2. **Prototype Linkage:** The newly created object's internal `[[Prototype]]` is bound to the constructor function's `prototype` property. If the constructor's `prototype` is a primitive value (including `null`), the engine safely falls back to binding the `[[Prototype]]` to `Object.prototype`.
3. **Context Binding:** The constructor function is executed with the newly created object bound to the `this` execution context. Any properties or methods added to `this` within the constructor are attached directly to the new object.
4. **Implicit Return:** The engine evaluates the constructor's return value. If the constructor explicitly returns a non-primitive object (e.g., an Object, Array, or Function), that object is returned, and the newly created object from Step 1 is discarded. If the constructor returns a primitive value (or returns nothing), the engine implicitly returns the newly created object referenced by `this`.

To visualize these internal mechanics, the behavior of the `new` operator can be represented by the following equivalent function:

```javascript theme={"dark"}
function simulateNew(constructor, ...args) {
  // Step 1 & 2: Create a new object and link its [[Prototype]]
  // Fall back to Object.prototype if constructor.prototype is a primitive or null
  let proto = constructor.prototype;
  if ((typeof proto !== 'object' && typeof proto !== 'function') || proto === null) {
    proto = Object.prototype;
  }
  const instance = Object.create(proto);
  
  // Step 3: Execute the constructor with 'this' bound to the new instance
  const result = constructor.apply(instance, args);
  
  // Step 4: Return the explicit object result, or the generated instance
  const isObject = typeof result === 'object' && result !== null;
  const isFunction = typeof result === 'function';
  
  return (isObject || isFunction) ? result : instance;
}
```

## Constructor Constraints

Not all functions can be invoked with the `new` operator. The engine enforces specific constraints based on the function type:

* **Arrow Functions:** Arrow functions cannot be used with the `new` operator. They lack the `[[Construct]]` internal method and do not possess a `prototype` property. Attempting to instantiate an arrow function will throw a `TypeError`.
* **ES6 Classes:** ES6 `class` constructors strictly require the `new` keyword. Unlike standard functions, invoking a class constructor as a standard function (without `new`) will throw a `TypeError`.

## The `new.target` Meta-Property

When a function is executed, the engine populates a pseudo-property named `new.target`. This allows developers to detect at runtime whether a function was invoked via the `new` operator.

* If the function was called with `new`, `new.target` holds a reference to the constructor function or class that was initially invoked. In the context of class inheritance, if a parent class is instantiated via a subclass (e.g., via `super()`), `new.target` within the parent constructor refers to the subclass that was initially invoked, not the parent constructor.
* If the function was called via standard invocation, `new.target` evaluates to `undefined`.

```javascript theme={"dark"}
function ConstructorCheck() {
  if (!new.target) {
    throw new TypeError("ConstructorCheck must be called with the 'new' operator.");
  }
  // Initialization logic proceeds here
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor JavaScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
