• <bdo id='CDzD8'></bdo><ul id='CDzD8'></ul>
    <tfoot id='CDzD8'></tfoot>
  • <small id='CDzD8'></small><noframes id='CDzD8'>

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

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

        JS [ES5] 如何用 setter 和 getter 分配对象?

        时间:2024-04-19
          <tbody id='ErNGh'></tbody>

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

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

            • <legend id='ErNGh'><style id='ErNGh'><dir id='ErNGh'><q id='ErNGh'></q></dir></style></legend>
            • <tfoot id='ErNGh'></tfoot>

                  <i id='ErNGh'><tr id='ErNGh'><dt id='ErNGh'><q id='ErNGh'><span id='ErNGh'><b id='ErNGh'><form id='ErNGh'><ins id='ErNGh'></ins><ul id='ErNGh'></ul><sub id='ErNGh'></sub></form><legend id='ErNGh'></legend><bdo id='ErNGh'><pre id='ErNGh'><center id='ErNGh'></center></pre></bdo></b><th id='ErNGh'></th></span></q></dt></tr></i><div id='ErNGh'><tfoot id='ErNGh'></tfoot><dl id='ErNGh'><fieldset id='ErNGh'></fieldset></dl></div>
                  本文介绍了JS [ES5] 如何用 setter 和 getter 分配对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  obj1 = Object.create({}, { property: { enumerable: true, value: 42 } })
                  
                  > obj1.property = 56
                  > 56
                  > obj1.property
                  > 42
                  

                  使用 use strict 会出错.

                  我想组合多个对象:使用 jQuery.extend():

                  I want to combine multiple objects: with jQuery.extend():

                  new_obj = $.extend(true, objN, obj1)
                  

                  使用 ES6 Object.assign:

                  with ES6 Object.assign:

                  new_obj = Object.assign({}, objN, obj1)
                  

                  在任何情况下,getter 都会变成常规属性,因此可以更改.如何避免?

                  In any case, the getter turns into a regular property, and therefore it can be changed. How to avoid it?

                  推荐答案

                  您可以编写自己的函数来复制属性:

                  You can write your own function that also copies over property attributes:

                  function extend(target, ...sources) {
                      for (let source of sources)
                          for (let key of Object.keys(source))
                              Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
                      return target;
                  }
                  

                  但请注意,Object.assign 不这样做是有充分理由的,如果 getter 和 setter 是闭包,它可能会产生奇怪的效果.

                  But notice there is good reason why Object.assign does not, it could have weird effects if getters and setters are closures.

                  这篇关于JS [ES5] 如何用 setter 和 getter 分配对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 `on()` 委托 `hover()` 函数 下一篇:JavaScript setInterval 没有正确绑定到正确的闭包

                  相关文章

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

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

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

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