Home > Web Front-end > JS Tutorial > body text

JS deep copy Object Array instance analysis_javascript skills

WBOY
Release: 2016-05-16 15:07:21
Original
1646 people have browsed it

This article analyzes the JS deep copy Object Array with examples. Share it with everyone for your reference, the details are as follows:

function cloneObj(o)
{
  var isArray = o instanceof Array;
  var isObject = o instanceof Object;
  if (!isObject) return o;
  var n = (isArray ? [] : {});
  for (var k in o) n[k] = cloneObj(o[k]);
  return n;
}

Copy after login

Problems encountered

typeof [] results in object
typeof {} results in object

[] instanceof Array results in true
{} instanceof Object results in true
The result of [] instanceof Object is also true

Explain that Array in JS is a subclass of Object.

Readers who are interested in more JavaScript-related content can check out the special topics on this site: "Summary of JavaScript search algorithm techniques", "Summary of JavaScript animation special effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm techniques", "Summary of JavaScript traversal algorithms and techniques" and "JavaScript Mathematics Summary of operation usage

I hope this article will be helpful to everyone in JavaScript programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!