一些新方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    // Object.assign({},this.user)
    //可以对对象进行复制

    //数组的去重和排序
    //在数组的原型上添加一个方法mySort
    Array.prototype.mySort = function () {
        //this就是对应的数组
        return Array.from(new Set(this)).sort((x, y) => {
            return x - y
        })
    }
    // const arr = new Array();
    const arr = [21,23,23,3344,23,23,3,3,4,4]
    const s = new Set(arr);
    console.log(s);  //Set(5) {21, 23, 3344, 3, 4}   
    console.log(Array.from(s)); // [21, 23, 3344, 3, 4]

// ----------------------------------------------------------------------

    //如何判断数组的数据类型
    // const arr = [1,2,3,4]
    // console.log(Array.isArray(arr))
    const obj = {}
    // console.log(obj.toString())
    // Object.prototype.toString()
    const arr = [1, 2, 3]
    console.log(arr.toString())
    console.log(Object.prototype.toString.call(arr))
    console.log(Object.prototype.toString.call(1))
    console.log(Object.prototype.toString.call('字符串'))
    console.log(Object.prototype.toString.call(true))
    console.log(Object.prototype.toString.call(null))
    //判断是否为数组的函数
    function isArray (arr) {
      /* if (Array.isArray) {
        return Array.isArray(arr)
      } else {
        return Object.prototype.toString.call(arr) === '[object Array]'
      } */
      return Array.isArray ? Array.isArray(arr) : Object.prototype.toString.call(arr) === '[object Array]'
    }

// ------------------------------------------------------------------------


    // 类数组对象转数组
    const obj = {
      "0": "值1",
      "1": "值2",
      "2": "值3",
      length: 3
    }
  
    console.log(Array.from(obj))
    console.log(Array.prototype.slice.call(obj))

    const arr = []
    for (var i = 0;i<obj.length; i++) {
      arr.push(obj[i])
    }

    console.log(arr)
    /* 
        基本数据类型
        String Number Boolean Undefined Null Object Symbol
    */

// ------------------------------------------------------------------------


    // 事件委托
    // 利用冒泡来实现的一种事件绑定方式

    // 事件委托可以给动态生成的元素上添加对应的数据,节省内存

    // 父/祖先.onclick = function (e) {
    //   var e = e || window.event
    //   var target = e.target || e.srcElement
    //   if (target.相关属性 === 相关值) {
        
    //   }
    // }

    // $('父/祖先').on('事件类型', '实际元素选择器', function () {

    // })

</script>
</html>

 

posted @ 2019-01-07 16:42  宝2333  阅读(101)  评论(0编辑  收藏  举报