{"id":3419,"date":"2016-03-15T23:27:32","date_gmt":"2016-03-15T23:27:32","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3419"},"modified":"2016-03-15T23:27:32","modified_gmt":"2016-03-15T23:27:32","slug":"lodash-extend-can-skip-properties","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/lodash-extend-can-skip-properties\/","title":{"rendered":"Why lodash &#8220;extend&#8221; or &#8220;assignTo&#8221; can skip properties"},"content":{"rendered":"<p>If you look at the prototype for an object in the browser console, you will see some proprties rendered in a light purple:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"460\" height=\"250\" class=\"alignnone size-full wp-image-3422\" src=\"http:\/\/172.104.26.128\/wp-content\/uploads\/2016\/03\/img_56e8501706f49.png\" alt=\"\" srcset=\"https:\/\/www.garysieling.com\/blog\/wp-content\/uploads\/2016\/03\/img_56e8501706f49.png 460w, https:\/\/www.garysieling.com\/blog\/wp-content\/uploads\/2016\/03\/img_56e8501706f49-300x163.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/p>\n<p>If you run &#8220;_.extend&#8221; on an object that has these, they will also disappear, like so:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"583\" height=\"98\" class=\"alignnone size-full wp-image-3424\" src=\"http:\/\/172.104.26.128\/wp-content\/uploads\/2016\/03\/img_56e85028159fe.png\" alt=\"\" srcset=\"https:\/\/www.garysieling.com\/blog\/wp-content\/uploads\/2016\/03\/img_56e85028159fe.png 583w, https:\/\/www.garysieling.com\/blog\/wp-content\/uploads\/2016\/03\/img_56e85028159fe-300x50.png 300w\" sizes=\"(max-width: 583px) 100vw, 583px\" \/><\/p>\n<p>However, you definitely can reference them directly (this one gives me an error, but obviously it&#8217;s actually there):<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"1904\" height=\"54\" class=\"alignnone size-full wp-image-3426\" src=\"http:\/\/172.104.26.128\/wp-content\/uploads\/2016\/03\/img_56e850b20e86b.png\" alt=\"\" srcset=\"https:\/\/www.garysieling.com\/blog\/wp-content\/uploads\/2016\/03\/img_56e850b20e86b.png 1904w, https:\/\/www.garysieling.com\/blog\/wp-content\/uploads\/2016\/03\/img_56e850b20e86b-300x9.png 300w, https:\/\/www.garysieling.com\/blog\/wp-content\/uploads\/2016\/03\/img_56e850b20e86b-1024x29.png 1024w, https:\/\/www.garysieling.com\/blog\/wp-content\/uploads\/2016\/03\/img_56e850b20e86b-768x22.png 768w, https:\/\/www.garysieling.com\/blog\/wp-content\/uploads\/2016\/03\/img_56e850b20e86b-1536x44.png 1536w, https:\/\/www.garysieling.com\/blog\/wp-content\/uploads\/2016\/03\/img_56e850b20e86b-1200x34.png 1200w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>In the <a href=\"https:\/\/github.com\/lodash\/lodash\/blob\/ddf9354d26f55ac4e8402b71770aaeac8ac6f093\/dist\/lodash.core.js\">lodash source<\/a>, &#8220;extend&#8221; is also known as assignIn:<\/p>\n<pre lang=\"javascript\">  var assignIn = createAssigner(function(object, source) {\n    copyObject(source, keysIn(source), object);\n  });\n<\/pre>\n<p>You can call _.keysIn directly, and see that the interesting properties are not returned:<\/p>\n<pre lang=\"javascript\">_.keysIn(React.Component.prototype)\n[\"isReactComponent\", \"setState\", \"forceUpdate\"]\n<\/pre>\n<p>Suprisingly, &#8220;hasOwnProperty&#8221; returns true for one of the &#8216;missing&#8217; functions:<\/p>\n<pre lang=\"javascript\">React.Component.prototype.hasOwnProperty(\"isMounted\")\ntrue\n<\/pre>\n<p>However, lodash will detect that an object is a prototype object by looking for a constructor:<\/p>\n<pre lang=\"javascript\"> var Ctor = object.constructor,\n          index = -1,\n          isProto = typeof Ctor == 'function' &amp;&amp; Ctor.prototype === object,\n          result = Array(length),\n          skipIndexes = length &gt; 0;\n<\/pre>\n<p>If it is a prototype object, it then skips the constructor automatically.<\/p>\n<p>The other missing properties are of a special class, called <a href=\"http:\/\/zduck.com\/2013\/non-enumerable-properties-in-javascript\/\">non-enumerable properties<\/a>.<\/p>\n<p>Armed with this knowledge, you can look into the React source, and find they use this all over the place, which explains the root problem:<\/p>\n<pre lang=\"javascript\">Object.defineProperty(element._store, 'validated', {\n  configurable: false,\n  enumerable: false,\n  writable: true,\n  value: false\n});\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you look at the prototype for an object in the browser console, you will see some proprties rendered in a light purple: If you run &#8220;_.extend&#8221; on an object that has these, they will also disappear, like so: However, you definitely can reference them directly (this one gives me an error, but obviously it&#8217;s &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/lodash-extend-can-skip-properties\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Why lodash &#8220;extend&#8221; or &#8220;assignTo&#8221; can skip properties&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302,343,455],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3419"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3419"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3419\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}