(es5实现es6+2)使用es5来实现es6的解构赋值

明白我们的需求是什么?

通过数组的形式去解构赋值数组时,按照数组下标的顺序进行赋值,
没有对应的值就为undefined
通过对象的形式去解构赋值对象时,按照相同的属性名进行赋值,
没有对应的值就为undefined,且可以给属性重命名。
// es5实现const关键字
var _const = (function() {
    var obj = {};
    return function(key, value) {
        if (value) {
            if (!obj[key]) {
                // 访问器属性
                Object.defineProperty(obj, key, {
                    get: function() {
                        return this["_" + key];
                    },
                    set: function(newValue) {
                        if (newValue == value) {
                            return (this.__proto__["_" + key] = newValue);
                        } else {
                            const message = `${key + "内存地址不能修改"}`;
                            throw new Error(message);
                        }
                    },
                });
                obj[key] = value;
            } else {
                throw new Error("不能重复声明" + key);
            }
            return obj;
        } else {
            throw new Error("声明常量必须赋值");
        }
    };
})();

// es5实现const关键字
var _let = (function() {
    var obj = {};
    return function(key, value) {
        if (!obj[key]) {
            Object.defineProperty(obj, key, {
                get: function() {
                    return this["_" + key];
                },
                set: function(newValue) {
                    return (this.__proto__["_" + key] = newValue);
                },
            });
            obj[key] = value;
        } else {
            throw new Error("不能重复声明" + key);
        }
        return obj;
    };
})();

var _var = function(key, value) {
    this.key = value;
};

// 根据传入的字符串判断变量用什么声明方式
var selectModifier = function(modifier) {
    if (modifier === "const") {
        return _const;
    } else if (modifier === "let") {
        return _let;
    } else {
        return _var;
    }
};

// 解构的实现
const _deconstruction = (function() {
    function isArray(modifier, value, originalValue) {
        var result = {};
        for (const index in value) {
            // 超出originalValue,js获取数组下标没有值,默认是undefined
            if (originalValue[index]) {
                result = selectModifier(modifier)(value[index], originalValue[index]);
            } else {
                result = selectModifier(modifier)(value[index], "undefined");
            }
        }
        return result;
    }

    function isObject(modifier, value, originalValue) {
        var result = {};
        for (const name in value) {
            if (originalValue.hasOwnProperty(name)) {
                result = selectModifier(modifier)(value[name], originalValue[name]);
            } else {
                result = selectModifier(modifier)(value[name], "undefined");
            }
        }
        return result;
    }
    return function(modifier, value, originalValue) {
        // 判断value是数组还是对象执行不同的逻辑
        return value instanceof Array ?
            isArray(modifier, value, originalValue) :
            value instanceof Object ?
            isObject(modifier, value, originalValue) :
            null;
    };
})();

function fn() {
    var obj = _deconstruction(
        "const", { name: "newName", d: "d" }, { name: "zhangsan", age: 20 }
    );
    console.log(obj.newName);
    console.log(obj.d);
    var arr = _deconstruction("const", ["a", "b", "c"], [1, 2]);
    console.log(arr.a);
    console.log(arr.b);
    console.log(arr.c);
}

fn();

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LiuJie_Boom

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值