# \_new

### 说明

&#x20;模拟实现`new`操作符

### 源码

<https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/function/_new>

### 用法

{% code title="\_new\.test.ts" %}

```typescript
import { _new } from "../../utility/function/_new";

describe('_new', () => {
  test('_new should return the instance of target constructor and received non parameter', () => {
    function Person() { }
    Person.prototype = {
      constructor: Person,
      say() {
        return 'Person';
      },
    };

    const received = [
      Person,
    ];
    const expected = [
      {
        constructor: Person,
        say: 'Person',
      },
    ];

    for (const [i, v] of received.entries()) {
      const result: any = _new(v);

      expect(result.constructor).toBe(expected[i]['constructor']);
      expect(result.say()).toBe(expected[i]['say']);
    }
  });

  test('_new should return the instance of target constructor and received many of parameters', () => {
    function Person(name: string, age: number) {
      this.name = name;
      this.age = age;
    }
    Person.prototype = {
      constructor: Person,
      say() {
        return 'Person';
      },
    };

    const received = [
      Person,
    ];
    const expected = [
      {
        name: 'ddzy',
        age: 21,
        constructor: Person,
        say: 'Person',
      },
    ];

    for (const [i, v] of received.entries()) {
      const result: any = _new(v, 'ddzy', 21);

      expect(result.name).toBe(expected[i]['name']);
      expect(result.age).toBe(expected[i]['age']);
      expect(result.constructor).toBe(expected[i]['constructor']);
      expect(result.say()).toBe(expected[i]['say']);
    }
  });
});
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ddzy.gitbook.io/ts-utility-plugins-docs/utility/utility-function/_new.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
