{"id":195,"date":"2015-04-08T21:21:47","date_gmt":"2015-04-08T21:21:47","guid":{"rendered":"https:\/\/www.tech.shinynewthings.com\/?p=195"},"modified":"2015-04-10T18:23:47","modified_gmt":"2015-04-10T18:23:47","slug":"javascript-object-defineproperty-eloquent-javascript-chapter-6","status":"publish","type":"post","link":"https:\/\/www.tech.shinynewthings.com\/?p=195","title":{"rendered":"Javascript &#8211; Object.defineProperty &#8211; Eloquent Javascript &#8211; Chapter 6"},"content":{"rendered":"<p>This is the first time I&#8217;ve used Object.defineProperty. Below, I&#8217;m using it to create a property on an object. The requirement is that the property is a function that is called without parens.<\/p>\n<p>It reveals some interesting things I&#8217;ve stumbled across recently. Console.log shows non enumerable properties when called on an object but those properties are not included in a for loop. Why? Because those properties have a property called enumerable that is set to false. If this property should show up in a for loop, include the following when using Object.defineProperty.<\/p>\n<p>If you stumble across this blog, these are my programming notes more than a blog. If you have any comments or questions, feel free to comment.<\/p>\n<pre><code>        enumerable: true\n<\/code><\/pre>\n<p>Here&#8217;s an example of the code I wrote for the Vector assignment in Chapter 6.<\/p>\n<pre><code>function Vector(x, y) {\n\n    \/\/ variables\n    this.x = x;\n    this.y = y;\n\n    \/\/ functions\n    this.powUp = function () {\n        return Math.pow(this.x, 2) + Math.pow(this.y,2);\n    }\n\n    this.plus = function (obj) {\n        return new Vector(this.x + obj.x, this.y + obj.y);\n    };\n\n    this.minus = function (x, y) {\n        return new Vector(this.x - obj.x, this.y - obj.y);\n    };\n\n    \/\/ requires parens to call \/ reset value;\n    \/\/this.length = function() {\n    \/\/  return Math.sqrt(this.powUp())\n    \/\/}\n\n    \/\/ removes the need for parens \/ adds code\n    \/\/ extend the object 1st param\n    \/\/ give it the name 'length'\n\n    Object.defineProperty (this, 'length', {\n        get: function () {\n            return  Math.sqrt(this.powUp())\n        }\n        \/\/set: function () {\n        \/\/  return  Math.sqrt(this.powUp())\n        \/\/}\n    });\n\n    \/\/ make it chainable\n    return this;\n};\n\n\n\/\/ Object.defineProperty Notes:\n\n    Object.defineProperty properties\n    get: function() {}        \/\/ function to returns value\n    set: function() {}        \/\/ function to set value\n    value: 0,                 \/\/ current value\n    enumerable: true,         \/\/ shows up in for loop\n    configurable: true,       \/\/ prop can be changed \/ deleted\n    writable: false           \/\/ can be modified\n\n    \/\/ Can be expressed as an object\n    \/*\n    Object.defineProperties (this, { \n        prop1: { get: function() {}, set: function() {} },\n        prop2: { get: function() {}, set: function() {} }\n    });\n    *\/\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is the first time I&#8217;ve used Object.defineProperty. Below, I&#8217;m using it to create a property on an object. The requirement is that the property is a function that is called without parens. It reveals some interesting things I&#8217;ve stumbled across recently. Console.log shows non enumerable properties when called on an object but those properties [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=195"}],"version-history":[{"count":4,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":202,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions\/202"}],"wp:attachment":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}