《饿了么大前端 Node.js 进阶教程》—Javascript 基础问题—引用传递

《饿了么大前端 Node.js 进阶教程》地址:https://github.com/ElemeFE/node-interview

1.如何编写一个 json 对象的拷贝函数

function clone(obj){

  var result;

  if (Array.isArray(obj)) {

    result = [];

    obj.forEach((item) => {

      result.push(clone(item));

    });        

  } else if (typeof obj === 'object') {

    result = {};

    for (key in obj) {

      result[key] = clone(obj[key]);

    }

  } else {

    result = obj;

  }

  return result;

}

如果是Date或者RegExp之类的类型,就得另加判断了

2.== 与 === 的区别

== 是两边值相等,===是不仅值相等类型也要相等

3.[1] == [1] 是 true 还是 false

想都不要想,肯定是false,因为是引用类型,比较的是地址;

原文地址:https://www.cnblogs.com/sungg/p/6797604.html