简体   繁体   English

创建JavaScript函数的替代方法

[英]Alternate ways of creating javascript functions

I came across the following script, and do not understand the first line. 我遇到以下脚本,但不了解第一行。 It apparently either returns JSON.stringify or an anonymous function. 它显然返回JSON.stringify或匿名函数。 Is it best to do it this way, or using the traditional the function printObj(obj) ? 最好是这样做,还是使用传统的function printObj(obj)呢? Also, where does JSON get defined? 另外,JSON在哪里定义? Thanks 谢谢

var printObj = typeof JSON != "undefined" ? JSON.stringify : function(obj) {
  var arr = [];
  $.each(obj, function(key, val) {
    var next = key + ": ";
    next += $.isPlainObject(val) ? printObj(val) : val;
    arr.push( next );
  });
  return "{ " +  arr.join(", ") + " }";
};

$("#log").append( printObj(object1) );
var printObj = // setting the variable printObj
               typeof JSON != 'undefined' // if this is true then set printObj to
                    ? JSON.stringify // <- ...this function
               : function( obj ) {
                   // otherwise let's build it ourselves...
               };

typeof JSON != 'undefined' will return true or false whether the identifier JSON has been defined. typeof JSON != 'undefined'将返回true或false,无论标识符JSON是否已定义。 typeof x will not cause a reference error if x has not been defined. 如果尚未定义x typeof x将不会导致引用错误。

JSON is already a function that exists in javascript. JSON已经是javascript中存在的功能。 (It has stringify and parse which you are most likely to use) (它具有stringifyparse ,您最有可能使用它)

The first line, just makes sure that a function exists for stringify, if it doesn't then it passes the object to a function function that attempts to parse the JSON object and return it as a JSON string. 第一行只是确保存在用于字符串化的函数,如果不存在,则将对象传递给试图解析JSON对象并将其作为JSON字符串返回的函数。

(I believe that the function "hack" that you are using, is done to fix an IE8 compatibility issue , where IE8 doesn't have a JSON object. You can avoid using that altogether if you set in your page: (我相信您正在使用的功能“ hack”是为了解决IE8兼容性问题 ,即IE8没有JSON对象。如果您在页面中进行设置,则可以完全避免使用它:

<meta http-equiv="X-UA-Compatible" content="IE=8" />

Also, the structure of that first line assignment, uses something called ternary operator assignment . 同样,该第一行分配的结构使用了一种称为三元运算符分配的东西。

For example: 例如:

var t = 1 == 1 ? 1 : 0;

JSON is a variable automatically defined by some of the Web browsers, like FireFox and Chrome. JSON是某些Web浏览器(例如FireFox和Chrome)自动定义的变量。 Old versions of IE do not define JSON, and a lot of annoying users still have these versions, and that is why it is defined manually in this case. IE的旧版本没有定义JSON,并且许多烦人的用户仍然拥有这些版本,这就是在这种情况下手动定义它的原因。

I think the best solution, is to just add a reference to the json library. 我认为最好的解决方案是仅添加对json库的引用。 Can be found here : http://www.json.org/js.html 可以在这里找到: http : //www.json.org/js.html

It defines a printObj function that can always be used : 它定义了始终可以使用的printObj函数:

  • if JSON.stringify is defined, printObj is this function 如果定义了JSON.stringify,则printObj是此函数
  • if JSON.stringify isn't available, the new function is used 如果JSON.stringify不可用,则使用新功能

Note that 注意

"I came across the following script, and do not understand the first line. It apparently either returns JSON.stringify or an anonymous function. " “我遇到了以下脚本,但不了解第一行。显然返回了JSON.stringify或一个匿名函数。

Seems that you do understand the first line. 似乎您确实了解第一行。

"Is it best to do it this way, or using the traditional the function printObj(obj) ? " “最好以这种方式执行此操作,还是使用传统的function printObj(obj)

Simply defining the function will have a different outcome, since the native JSON.stringify would be ignored, and should be preferred over a shim. 简单定义函数将产生不同的结果,因为原生JSON.stringify将被忽略,并且应优先于填充程序。

The alternate given in the question isn't a full shim, and even if it was, the native would most certainly perform better. 问题中给出的替代方案不是完整的填充,即使是,它也肯定会表现得更好。

"Also, where does JSON get defined?" “而且,JSON在哪里定义?”

It's defined in ECMAScript 5 environments by default. 默认情况下,它是在ECMAScript 5环境中定义的。

Using this syntax, you're able to create a function at the same time as checking for the existence of a suitable built in method. 使用此语法,您可以在检查合适的内置方法存在的同时创建一个函数。 JSON is an object that some modern browsers have built into the engine, and much like Window or Location. JSON是一些现代浏览器已内置到引擎中的对象,与Window或Location十分相似。 Older browsers wouldn't have JSON, and and therefore would return undefined, and allowing the custom anonymous function to be used. 较早的浏览器没有JSON,因此将返回undefined,并允许使用自定义匿名函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM