Skip to content

Complement task solution with additional details #1442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,55 @@ Here's how `new user.constructor('Pete')` works:
2. Then it follows the prototype chain. The prototype of `user` is `User.prototype`, and it also has nothing.
3. The value of `User.prototype` is a plain object `{}`, its prototype is `Object.prototype`. And there is `Object.prototype.constructor == Object`. So it is used.

At the end, we have `let user2 = new Object('Pete')`. The built-in `Object` constructor ignores arguments, it always creates an empty object -- that's what we have in `user2` after all.
At the end, we have `let user2 = new Object('Pete')`. The built-in `Object` constructor treats passed arguments in regard with their type which may result in the following outcomes.

1. Primitive wrapper object will be created if primitive value was passed:

```js
// String
let strObj = new Object('str');
typeof strObj; // "object"
typeof strObj.valueOf(); // "string"

// Number
let numObj = new Object(3);
typeof numObj; // "object"
typeof numObj.valueOf(); // "number"

// Boolean
let boolObj = new Object(true);
typeof boolObj; // "object"
typeof boolObj.valueOf(); // "boolean"

// Symbol
let symObj = new Object(Symbol('s'));
typeof symObj; // "object"
typeof symObj.valueOf(); // "symbol"

// BigInt
let bigIntObj = new Object(BigInt(2));
typeof bigIntObj; // "object"
typeof bigIntObj.valueOf(); // "bigint"
```

2. For user created objects(with custom constructor) passed in, call the built-in `Object` constructor will simply return those objects identity(not recreate):

```js
function Foo() {}
let f = new Foo();

let k = new Object(f);
k instanceof Foo; // true
f === k; // true
// both of f and k points on the same object
```

3. New plain object will be created in case if `null`, `undefined` or nothing was passed:

```js
new Object(null).toString(); // [object Object]
new Object(undefined).toString(); // [object Object]
new Object().toString(); // [object Object]
```

So, `user2` will be initialized with the primitive wrapper object around `String` type, therefore `user2.name === undefined` since there is no `name` property exists on `String.prototype`