针对本地JS角JSON相关的功能 [英] angular json related functions against native js

查看:135
本文介绍了针对本地JS角JSON相关的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

angular.toJson( obj, pretty );
angular.fromJson( json );

VS

JSON.stringify( obj )
JSON.parse( json )

我用原生的,但开始使用角度药粥一致性。其他原因使用那些?

I used to use native ones, but started to use angular ones for consistency. Any other reasons to use those?

推荐答案

我首先想到的是它的一些相关的测试目的(同样的情况下,使用$窗口,而不是窗口)。但寻找到源$ C ​​$ C后:的https:/ /github.com/angular/angular.js/blob/master/src/Angular.js#L977

My first thought was it's related to some test purposes(same case with using $window instead of window). But after looking into source code: https://github.com/angular/angular.js/blob/master/src/Angular.js#L977

function toJson(obj, pretty) {
  if (typeof obj === 'undefined') return undefined;
  if (!isNumber(pretty)) {
    pretty = pretty ? 2 : null;
  }
  return JSON.stringify(obj, toJsonReplacer, pretty);
}

看起来像它的情况下,具有未定义的对象作为参数的简单包装。

Looks like it's a simple wrap for case with undefined object as param.

同为 fromJson https://github.com/angular/angular.js/blob/master/src/Angular.js#L998

function fromJson(json) {
  return isString(json)
      ? JSON.parse(json)
      : json;
}

所以,一般来说,它只是删除从应用code检查到框架code。

so, generally, it's just to remove that checking from app code into framework code.

这篇关于针对本地JS角JSON相关的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆