{"id":1241,"date":"2022-08-30T15:14:44","date_gmt":"2022-08-30T15:14:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/undefined-property-after-passing-an-object-as-a-parameter-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:44","modified_gmt":"2022-08-30T15:14:44","slug":"undefined-property-after-passing-an-object-as-a-parameter-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/undefined-property-after-passing-an-object-as-a-parameter-collection-of-common-programming-errors\/","title":{"rendered":"Undefined property after passing an object as a parameter-Collection of common programming errors"},"content":{"rendered":"<p>Because variables are not properties <em>(except WRT global variables\/properties)<\/em>. You&#8217;re creating a global variable <code>bar<\/code> by not using the <code>var<\/code> statement. Even if you use <code>var<\/code>, it doesn&#8217;t show up on the object being constructed.<\/p>\n<p>Since you&#8217;re using <code>new<\/code>, you can set the property on <code>this<\/code>.<\/p>\n<pre><code>this.bar = \"ok\"\n<\/code><\/pre>\n<p>So full code is&#8230;<\/p>\n<pre><code>function foo() {\n    this.bar = 'ok';\n    new baz(this);\n    }\n\nfunction baz(foo) {\n    alert(foo.bar);\n    }\n\nnew foo();\n<\/code><\/pre>\n<p>By the way, the <code>new<\/code> keyword is wasted on <code>baz<\/code> since you&#8217;re not retaining any object created.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:11:43. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Because variables are not properties (except WRT global variables\/properties). You&#8217;re creating a global variable bar by not using the var statement. Even if you use var, it doesn&#8217;t show up on the object being constructed. Since you&#8217;re using new, you can set the property on this. this.bar = &#8220;ok&#8221; So full code is&#8230; function foo() [&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-1241","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1241","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=1241"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1241\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}