Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript json模式的构造函数_Javascript_Json_Oop - Fatal编程技术网

Javascript json模式的构造函数

Javascript json模式的构造函数,javascript,json,oop,Javascript,Json,Oop,有没有从JSON模式构建对象构造函数的方法?我想创建一个与我的应用程序名称空间关联的json模式,我可以编辑它一次,并更改对象的属性(在运行前) 我知道你可以编写一个伪经典的对象构造函数,比如 var Note = function(input){ var title = input }; var newNote = new Note("test title"); 可以从json创建类似的结构吗?这样我就可以写: var Note = { "title":"" }; var newN

有没有从JSON模式构建对象构造函数的方法?我想创建一个与我的应用程序名称空间关联的json模式,我可以编辑它一次,并更改对象的属性(在运行前)

我知道你可以编写一个伪经典的对象构造函数,比如

var Note = function(input){ 
  var title = input
};
var newNote = new Note("test title");
可以从json创建类似的结构吗?这样我就可以写:

var Note = {
  "title":""
};
var newNote = new Note();
newNote.title = "test title"
我理解上述内容在语法上是错误的,但我想举例说明:

var notes = {
  "NotesList":[{
    "title":"note1",
    "content":"test content"
  }]
}
var newNote = new Note();
notes.NotesList.add(newNote);
newNote.title = "new title";

因此,我可以将我的所有对象及其所有子对象基于从json模式创建的对象模板。如果这是不可能的,你能推荐一个更好的方法吗?

我找到了我想要的答案。我的最终目标是创建一个包含子对象数组的对象,每个子对象可以有子对象等。然后,该对象将用作我整个应用程序的命名空间,如:

var myobj = {};
我想使用JSON指定此对象的属性,然后根据这些属性构建构造函数,如:

var myObj = {
"name": "genericname",
"id": "uniqueid",
"children": [
    {
        "grandchildren": [
            {
                "name": "child",
                "age": "0"
            }
        ]
    }
]
};
我最后做的是,从中构建构造函数,然后将它们克隆到我的新对象中,这样它们就可以有如下默认值:

myObj.Child = function(){
  var child = myObj.children[0];
  return child;
  //this is the unmodified constructor child
};
var myObj.createChild = function(){
  var newChild = new Clone(myObj.Child());
  //do stuff to this is new child which can be modified without changing the original
  return newChild;
};

我不确定我是否完全理解了你的问题。但是,如果您希望将函数转换为JSON,那么是的,这绝对是可能的

您所需要做的就是使用类似的JavaScript解析器,它使用生成JSON格式的构造函数的。例如:

var ast = acorn.parse(Note);
var json = JSON.stringify(ast, null, 4);
alert(json);

function Note(input) {
    var title = input;
}
请看演示。上述代码生成以下输出:

{
    "type": "Program",
    "start": 0,
    "end": 47,
    "body": [
        {
            "type": "FunctionDeclaration",
            "start": 0,
            "end": 47,
            "id": {
                "type": "Identifier",
                "start": 9,
                "end": 13,
                "name": "Note"
            },
            "params": [
                {
                    "type": "Identifier",
                    "start": 14,
                    "end": 19,
                    "name": "input"
                }
            ],
            "body": {
                "type": "BlockStatement",
                "start": 21,
                "end": 47,
                "body": [
                    {
                        "type": "VariableDeclaration",
                        "start": 27,
                        "end": 44,
                        "declarations": [
                            {
                                "type": "VariableDeclarator",
                                "start": 31,
                                "end": 44,
                                "id": {
                                    "type": "Identifier",
                                    "start": 31,
                                    "end": 36,
                                    "name": "title"
                                },
                                "init": {
                                    "type": "Identifier",
                                    "start": 39,
                                    "end": 44,
                                    "name": "input"
                                }
                            }
                        ],
                        "kind": "var"
                    }
                ]
            }
        }
    ]
}
您可以将上述AST保存在JSON文件中,并在需要时加载它。您可以使用将AST转换回JavaScript,如下所示:

alert(escodegen.generate(ast));

请看演示。然后,您可以
eval
生成的代码,并根据需要使用
Note
构造函数。

这实际上不是JSON,更不用说JSON模式了,它只是Javascript代码。因此,举例来说,引号是不必要的。如果将
.add
更改为
.push
,代码就会按原样运行。第三个示例的真正目的是从模式文件中读取json并从中创建一个类。为什么不能直接使用解析后的json呢?在有数据或列表的对象后面不需要有类。我想使用JSON创建一个类,我可以调用一个构造函数来创建具有这些缺省属性的新实例。您可以考虑这是一个设计时函数,并使用像T4模板这样的东西来生成伪经典版本。(或其他)从JSON模式作为输入。只是一个想法。