• Object 和 JSON 区别联系


    JavaScript                                                       

    Object-based

    JavaScript is almost entirely object-based.

    Object                                                              

    name

    Object property names are string keys.

    syntax

    dot notation (obj.x = 10)

    bracket notation (obj['x'] = 10)

    value

    value can be a string in double quotes, or a number, or true or false or null, or an object or an array.

    enumerated

    for...in

    JSON (JavaScript Object Notation)                 

    structures

    A collection of name/value pairs.  {}

    An ordered list of values.      []

    JSON 和 object 区别                                            

    var o = {"a":1,"b":2};
    console.dir(typeof(o));// object
    
    var a = [{"a":1},{"b":2}];
    console.dir(typeof(a));// object

    json 是对 object 的 描述

    json 不是对象

    json object 是 object

    json 字符串 是 string

    JSON object 和 JSON string 区别                        

    JSON object 有懒模式:可以缺省name的"" ,该name 不会被解析 。

    var a = "abc";
    var o = {a:1};//不标准写法;a不会被abc代替。
    console.dir(o.a);//1
    console.dir(o.abc);//undefined

    JSON string 格式必须是标准格式,否则不能解析。

    var o = {a:1};
    var ostr = JSON.stringify(o);
    console.dir(ostr); // {"a":1}     标准
                       // "{"a":1}"
    console.dir(JSON.parse('{"a":1}'));   // Object
    console.dir(JSON.parse("{"a":1}")); // Object
    console.dir(JSON.parse("{a:1}"));     // SyntaxError

     通过 Function 将懒模式的字符串转换为 JSON object

    var ostr = "{a:1,b:2}";
    var o = (new Function("return "+ostr))();
    console.dir(o); // Object
    console.dir(JSON.stringify(o)); // {"a":1,"b":2}    
  • 相关阅读:
    clientX和clientY属性需要注意的地方
    事件冒泡 --- 仿select下拉框
    body和document的梗
    完美运动框架
    仿flash运动框架
    多物体运动框架
    Computed Styles
    悬浮框
    【一起驴友】公司笔试
    Client Dimensions , offsetHeight , scrollTop 属性详解
  • 原文地址:https://www.cnblogs.com/zno2/p/4672904.html
Copyright © 2020-2023  润新知