<legend id='gx56S'><style id='gx56S'><dir id='gx56S'><q id='gx56S'></q></dir></style></legend>

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

        • <bdo id='gx56S'></bdo><ul id='gx56S'></ul>
        <tfoot id='gx56S'></tfoot>

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

        Javascript中new Boolean()的目的是什么?

        时间:2023-09-04
        • <bdo id='UQXxl'></bdo><ul id='UQXxl'></ul>
            <legend id='UQXxl'><style id='UQXxl'><dir id='UQXxl'><q id='UQXxl'></q></dir></style></legend>

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

                  <tfoot id='UQXxl'></tfoot>
                    <tbody id='UQXxl'></tbody>
                  <i id='UQXxl'><tr id='UQXxl'><dt id='UQXxl'><q id='UQXxl'><span id='UQXxl'><b id='UQXxl'><form id='UQXxl'><ins id='UQXxl'></ins><ul id='UQXxl'></ul><sub id='UQXxl'></sub></form><legend id='UQXxl'></legend><bdo id='UQXxl'><pre id='UQXxl'><center id='UQXxl'></center></pre></bdo></b><th id='UQXxl'></th></span></q></dt></tr></i><div id='UQXxl'><tfoot id='UQXxl'></tfoot><dl id='UQXxl'><fieldset id='UQXxl'></fieldset></dl></div>
                  本文介绍了Javascript中new Boolean()的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有什么用:

                  var flag = new Boolean(false); 
                  

                  相比:

                  var flag = false;
                  

                  你什么时候真正使用new Boolean?

                  推荐答案

                  全局函数Boolean()可以在没有new的情况下进行类型转换,例如

                  The global function Boolean() can be used for type casting when called without new, eg

                  var foo = Boolean(bar); // equivalent to `var foo = !!bar`
                  

                  当使用new调用时,会额外创建一个包装对象,这意味着你可以为对象分配任意属性:

                  When called with new, a wrapper object will be created additionally, which means that you can assign arbitrary properties to the object:

                  var foo = new Boolean(bar); // equivalent to `var foo = Object(Boolean(bar));`
                  foo.baz = 'quux';
                  alert(foo.baz);
                  

                  这对于原始值是不可能的,因为原始值不能保存属性:

                  This is not possible with primitive values as primitives can't hold properties:

                  var foo = true;
                  foo.baz = 'quux';
                  alert(foo.baz); // `foo.baz` is `undefined`
                  

                  将属性分配给基元不会因为自动装箱而产生错误,即

                  Assigning a property to a primitive doesn't produce an error because of auto-boxing, ie

                  foo.baz = 'quux';
                  

                  将被解释为

                  // create and immediately discard a wrapper object:
                  (new Boolean(foo)).baz = 'quux';
                  

                  要取回原始值,您必须调用 valueOf() 方法.如果您想实际使用包装的值,则需要这样做,因为对象在布尔上下文中总是评估为 true - 即使包装的值是 false.

                  To get the primitive value back, you'll have to invoke the valueOf() method. This is needed if you want to actually use the wrapped value, because objects always evaluate to true in boolean contexts - even if the wrapped value is false.

                  我从未遇到过能够将属性分配给布尔值的有用应用程序,但在需要引用原始值的情况下,装箱可能很有用.

                  I've never come across a useful application of being able to assign properties to booleans, but boxing might be useful in cases where a reference to a primitive value is needed.

                  这篇关于Javascript中new Boolean()的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:何时在 JavaScript 中使用双非 (!!) 运算符 下一篇:在 javascript 中,空字符串是否总是错误的布尔值?

                  相关文章

                  • <bdo id='4TLYt'></bdo><ul id='4TLYt'></ul>
                1. <legend id='4TLYt'><style id='4TLYt'><dir id='4TLYt'><q id='4TLYt'></q></dir></style></legend>

                  <small id='4TLYt'></small><noframes id='4TLYt'>

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