<bdo id='A5owe'></bdo><ul id='A5owe'></ul>

    1. <legend id='A5owe'><style id='A5owe'><dir id='A5owe'><q id='A5owe'></q></dir></style></legend>

        <small id='A5owe'></small><noframes id='A5owe'>

        <tfoot id='A5owe'></tfoot>

        <i id='A5owe'><tr id='A5owe'><dt id='A5owe'><q id='A5owe'><span id='A5owe'><b id='A5owe'><form id='A5owe'><ins id='A5owe'></ins><ul id='A5owe'></ul><sub id='A5owe'></sub></form><legend id='A5owe'></legend><bdo id='A5owe'><pre id='A5owe'><center id='A5owe'></center></pre></bdo></b><th id='A5owe'></th></span></q></dt></tr></i><div id='A5owe'><tfoot id='A5owe'></tfoot><dl id='A5owe'><fieldset id='A5owe'></fieldset></dl></div>
      1. 如何在 JavaScript 中创建 CoffeeScript 风格的存在运算符?

        时间:2024-04-19
        <legend id='kFvsx'><style id='kFvsx'><dir id='kFvsx'><q id='kFvsx'></q></dir></style></legend>

        • <tfoot id='kFvsx'></tfoot>
            <bdo id='kFvsx'></bdo><ul id='kFvsx'></ul>
              <tbody id='kFvsx'></tbody>

              <i id='kFvsx'><tr id='kFvsx'><dt id='kFvsx'><q id='kFvsx'><span id='kFvsx'><b id='kFvsx'><form id='kFvsx'><ins id='kFvsx'></ins><ul id='kFvsx'></ul><sub id='kFvsx'></sub></form><legend id='kFvsx'></legend><bdo id='kFvsx'><pre id='kFvsx'><center id='kFvsx'></center></pre></bdo></b><th id='kFvsx'></th></span></q></dt></tr></i><div id='kFvsx'><tfoot id='kFvsx'></tfoot><dl id='kFvsx'><fieldset id='kFvsx'></fieldset></dl></div>

                  <small id='kFvsx'></small><noframes id='kFvsx'>

                  本文介绍了如何在 JavaScript 中创建 CoffeeScript 风格的存在运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  CoffeeScript 将 user?.id 变成

                  CoffeeScript turns user?.id into

                  if (typeof user !== "undefined" && user !== null) {
                     user.id;
                  }
                  

                  是否可以创建一个 JavaScript 函数 exists 来做类似的事情?即

                  Is it possible to create a JavaScript function exists that would do something similar? i.e.

                  exists(user).id
                  

                  将导致 user.idnull

                  如果一个函数接受另一个参数,即 exists(user, 'id') 会更容易,但这看起来不太好.

                  It would be easier if a function accepts another parameter, i.e. exists(user, 'id'), but that wouldn't look as nice.

                  推荐答案

                  不,你不能产生这样的功能.问题在于:

                  No, you can't produce such a function. The problem is that this:

                  any_function(undeclared_variable)
                  

                  如果 undeclared_variable 没有在任何地方声明,

                  将产生一个 ReferenceError.例如,如果您运行此独立代码:

                  will produce a ReferenceError if undeclared_variable was not declared anywhere. For example, if you run this stand alone code:

                  function f() { }
                  f(pancakes);
                  

                  你会得到一个 ReferenceError 因为 pancakes 没有在任何地方声明.演示:http://jsfiddle.net/ambiguous/wSZaL/

                  you'll get a ReferenceError because pancakes was not declared anywhere. Demo: http://jsfiddle.net/ambiguous/wSZaL/

                  但是,typeof operator 可用于尚未声明的内容,因此:

                  However, the typeof operator can be used on something that has not been declared so this:

                  console.log(typeof pancakes);
                  

                  将简单地在控制台中记录一个 undefined.演示:http://jsfiddle.net/ambiguous/et2Nv/

                  will simply log an undefined in the console. Demo: http://jsfiddle.net/ambiguous/et2Nv/

                  如果您不介意可能的 ReferenceErrors,那么您的问题中已经有了必要的功能:

                  If you don't mind possible ReferenceErrors then you already have the necessary function in your question:

                  function exists(obj, key) {
                      if (typeof obj !== "undefined" && obj !== null)
                          return obj[key];
                      return null; // Maybe you'd want undefined instead
                  }
                  

                  或者,由于您不需要能够在此处对未声明的变量使用 typeof,您可以将其简化为:

                  or, since you don't need to be able to use typeof on undeclared variables here, you can simplify it down to:

                  function exists(obj, key) {
                      if(obj != null)
                        return obj[key];
                      return null;
                  }
                  

                  注意对 != 的更改,undefined == null 为真,即使 undefined === null 不是.

                  Note the change to !=, undefined == null is true even though undefined === null is not.

                  这篇关于如何在 JavaScript 中创建 CoffeeScript 风格的存在运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如果 src 是 base64 字符串,如何获取新创建的 Image() 的文件大小? 下一篇:如何在 atom 的包内设置断点?

                  相关文章

                    <tfoot id='9H0yQ'></tfoot>

                    <legend id='9H0yQ'><style id='9H0yQ'><dir id='9H0yQ'><q id='9H0yQ'></q></dir></style></legend>

                    <small id='9H0yQ'></small><noframes id='9H0yQ'>

                    • <bdo id='9H0yQ'></bdo><ul id='9H0yQ'></ul>
                  1. <i id='9H0yQ'><tr id='9H0yQ'><dt id='9H0yQ'><q id='9H0yQ'><span id='9H0yQ'><b id='9H0yQ'><form id='9H0yQ'><ins id='9H0yQ'></ins><ul id='9H0yQ'></ul><sub id='9H0yQ'></sub></form><legend id='9H0yQ'></legend><bdo id='9H0yQ'><pre id='9H0yQ'><center id='9H0yQ'></center></pre></bdo></b><th id='9H0yQ'></th></span></q></dt></tr></i><div id='9H0yQ'><tfoot id='9H0yQ'></tfoot><dl id='9H0yQ'><fieldset id='9H0yQ'></fieldset></dl></div>