{"id":1115,"date":"2022-08-30T15:12:38","date_gmt":"2022-08-30T15:12:38","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/javascript-prototype-quirk-can-anyone-explain-this-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:12:38","modified_gmt":"2022-08-30T15:12:38","slug":"javascript-prototype-quirk-can-anyone-explain-this-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/javascript-prototype-quirk-can-anyone-explain-this-collection-of-common-programming-errors\/","title":{"rendered":"Javascript Prototype Quirk &#8211; Can Anyone Explain This?-Collection of common programming errors"},"content":{"rendered":"<p>The problem here is that you are returning a property named <code>prototype<\/code> with the value parent, which is an object, so you are returning the pair <code>prototype = {name = 'keith'}<\/code> and when you call new obj, you are adding to the prototype of <code>a<\/code> a new property named <code>prototype<\/code>.<\/p>\n<p>You just need a bit change, I need this is what you are trying to do. This will work, just be carefull with overload properties.<\/p>\n<pre><code>var obj = function(parent){\n   for(var propt in parent){\n      this[propt] = parent[propt];\n   }\n}\n\nvar me = { name: 'keith' };\nvar a = new obj(me);\nconsole.log(a);\n\/\/ =&gt; Object { name=\"keith\"}\nconsole.log(a.name);\n\/\/ =&gt; \"keith\"\n<\/code><\/pre>\n<p>Edit: If you are looking for inheritance using prototype, you should read this or try TypeScript, a new javascript typed and OO library<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:29:56. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>The problem here is that you are returning a property named prototype with the value parent, which is an object, so you are returning the pair prototype = {name = &#8216;keith&#8217;} and when you call new obj, you are adding to the prototype of a a new property named prototype. You just need a bit [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1115","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1115"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1115\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}