京东前端二面高频面试题

寄生组合继承

题目描述:实现一个你认为不错的 js 继承方式

实现代码如下:

function Parent(name) {
   
  this.name = name;
  this.say = () => {
   
    console.log(111);
  };
}
Parent.prototype.play = () => {
   
  console.log(222);
};
function Children(name) {
   
  Parent.call(this);
  this.name = name;
}
Children.prototype = Object.create(Parent.prototype);
Children.prototype.constructor = Children;
// let child = new Children("111");
// // console.log(child.name);
// // child.say();
// // child.play();

前端进阶面试题详细解答

----问题知识点分割线----

数组去重

ES5 实现:

function unique(arr) {
   
    var res = arr.filter(function(item, index, array) {
   
        return array.indexOf(item) === index
    })
    return res
}

ES6 实现:

var unique = arr => [...new Set(arr)]

----问题知识点分割线----

树形结构转成列表

题目描述:

[
    {
   
        id: 1,
        text: '节点1',
        parentId: 0,
        children: [
            {
   
                id:2,
                text: '节点1_1',
                parentId:1
            }
        ]
    }
]
转成
[
    {
   
        id: 1,
        text: '节点1',
        parentId: 0 //这里用0表示为顶级节点
    },
    {
   
        id: 2,
        text: '节点1_1',
        parentId: 1 //通过这个字段来确定子父级
    }
    ...
]


实现代码如下:

function treeToList(data) {
   
  let res = [];
  const dfs = (tree) => {
   
    tree.forEach((item) => {
   
      if (item.children) {
   
        dfs(item.children);
        delete item.children;
      }
      res.push(item);
    });
  };
  dfs(data);
  return
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值