• <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 分配对象?

        JS [ES5] How to assign objects with setters and getters?(JS [ES5] 如何用 setter 和 getter 分配对象?)
          <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 分配对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)

                  <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>

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

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