• [js]Object.defineProperty等几个js特殊方法


    Object.defineProperty

    var obj = {
        test: "hello"
    };
    
    obj['favor'] = 'orange';
    //对象已有的属性添加特性描述
    // Object.defineProperty(obj, "test", {
    //     configurable: true,
    //     enumerable: true,
    //     writable: true
    // });
    //对象新添加的属性的特性描述
    Object.defineProperty(obj, "age", {
        configurable: true,
        value: 22,
        enumerable: true,
        writable: false
    });
    
    obj['age'] = 10;
    console.log(obj);
    

    检测类型Object.prototype.toString.call

    Object.prototype.toString.call();
    
    Object.prototype.toString.call([]); //"[object Array]"
    Object.prototype.toString.call({}); //"[object Object]"
    Object.prototype.toString.call(null); //"[object Null]"
    Object.prototype.toString.call(undefined); //"[object Undefined]"
    

    转数组

    Array.prototype.slice().call(likeArr);
    
    let likeArrToArr = function (likeArr) {
        var arr = [];
        try {
            arr = Array.prototype.slice.call(likeArr);
        } catch (e) {
            for (var i = 0; i < likeArr.length; i++) {
                arr[arr.length] = likeArr[i];
            }
        }
    
        return arr;
    };
    
    
    var arr = {length: 2, 0: 'first', 1: 'second'};
    console.log(likeArrToArr(arr));
    
    var arr2 = [0,11,22,33];
    console.log(likeArrToArr(arr2));
    
  • 相关阅读:
    XMAPP搭建DVWA靶机
    博客滑动相册封面导航教程
    MySQL-分页与排序
    MySQL-子查询
    java方法
    JSP小结
    javaScript入门介绍2
    Codeforces Global Round 13
    第一章、OS引论1
    JavaScript入门介绍2021/02/27
  • 原文地址:https://www.cnblogs.com/iiiiiher/p/9999642.html
Copyright © 2020-2023  润新知