针对本地JS角JSON相关的功能 [英] angular json related functions against native js
问题描述
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屋!