javascript - JS : json, 动态方法生物和闭包

标签 javascript json

抱歉,标题不是很清楚,但我不知道问题出在哪里。

所以,我需要编写一个函数,它从 json 创建 js 对象,对于以下划线开头的字段,它必须创建 setter 和 getter。

这是测试 json:

{
"_language":null,
"_country":null
}

这是我写的函数

function jsonToObject(json){
    return JSON.parse(json,function(key,value){
        if (value && typeof value === 'object') {
            return (function(value){
                var replacement = {};
                for (var k in value) {
                    if (Object.hasOwnProperty.call(value, k)) {
                        //if this is private field 
                        if (k.lastIndexOf("_", 0) === 0){
                            replacement[k]=value[k];
                            var name=k.substring(1);
                            var getName="get"+name.charAt(0).toUpperCase() + name.slice(1);
                            replacement.constructor.prototype[getName]=function(){
                                return this[k];
                            };
                            var setName="set"+name.charAt(0).toUpperCase() + name.slice(1);
                            replacement.constructor.prototype[setName]=function(newValue){
                                this[k]=newValue;
                            };
                        //if this is public field
                        }else{
                            replacement[k]=value[k];
                        }
                    }
                }
                return replacement;
            }(value));
        }
        return value;
    });
}

我是这样测试的:

var test1=jsonToObject(data);
test1.setLanguage("11");
console.log("Point A:"+test1.getLanguage());//ouput 11
var test2=jsonToObject(data);
test2.setLanguage("22");
console.log("Point B:"+test2.getLanguage())//output 22
console.log("Point C:"+test1.getLanguage());//output function (a){this[c]=a}
console.log("Point D:"+test2.getLanguage())//output 22

问题出在 C 点 - 输出必须是 11。但是它的输出是函数...(此代码经过优化,这就是它看起来很模糊的原因)。我的错误在哪里?

最佳答案

setter 和 getter 重复定义

function jsonToObject(json) {
    return JSON.parse(json, function (key, value) {
        if (value && typeof value === 'object') {
            return (function (value) {
                var replacement = {};
                for (var k in value) {
                    if (Object.hasOwnProperty.call(value, k)) {
                        //if this is private field 
                        if (k.lastIndexOf("_", 0) === 0) {
                            replacement[k] = value[k];
                            var name = k.substring(1);
                            var getName = "get" + name.charAt(0).toUpperCase() + name.slice(1);
                            //
                            // check defined
                            //
                            console.log(replacement.constructor.prototype[getName]);
                            //
                            // if defined function, prevent override
                            //
                            if (!/function/.test(typeof replacement.constructor.prototype[getName])) {
                                replacement.constructor.prototype[getName] = function () {
                                    return this[k];
                                };
                            }
                            var setName = "set" + name.charAt(0).toUpperCase() + name.slice(1);
                            // check defined
                            console.log(replacement.constructor.prototype[setName]);
                            // if defined function, prevent override
                            if (!/function/.test(typeof replacement.constructor.prototype[setName])) {
                                replacement.constructor.prototype[setName] = function (newValue) {
                                    this[k] = newValue;
                                };
                            }
                            //if this is public field
                        } else {
                            replacement[k] = value[k];
                        }
                    }
                }
                return replacement;
            }(value));
        }
        return value;
    });
}

关于javascript - JS : json, 动态方法生物和闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39640898/

相关文章:

javascript - jquery.each 在 jquery.load 之后不工作

javascript - 使用 JavaScript 附加到 JSON 对象

java - 我应该如何在 Spring boot 中配置 httpMessageConverters 以将消息转换为所需的格式?

java - JSONObject - 如何获取值?

php - 正确编码和解码 json 结果(php 到 js)

javascript - 删除和添加回元素后,JQuery 单击功能不起作用

javascript - jquery 倒计时器 - 按键时重置

php - 在 <div> 中每 2500 个字符换行文本以使用 PHP 或 javascript 进行分页

java - getJSON JavaScript 函数返回 undefined object

javascript - 路由问题,不收集 req.params