Skip to content
Open
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ var newObject = deepClone(obj);

```javascript
function deepClone(object){
var newObject = {};
var newObject = Array.isArray(object) ? [] : {};
for(var key in object){
if(typeof object[key] === 'object' && object[key] !== null ){
newObject[key] = deepClone(object[key]);
Expand Down Expand Up @@ -1201,6 +1201,8 @@ var personalDetail = {
```
So when we do deep clone then we should copy every property (including the nested object).

Without `Array.isArray()` check, `[1, 2, 3]` was silently cloned as `{ "0": 1, "1": 2, "2": 3 }` since both arrays and objects return `"object"` for `typeof`,fixed by initializing `newObject` as `[]` when the input is an array

</details>

## Question 30. Best way to detect `undefined` object property in JavaScript.
Expand Down