Sophivorus Sophivorus - 1 year ago 87
Javascript Question

Undefined property after passing an object as a parameter

function foo() {
bar = 'ok';
new baz( this );

function baz( foo ) {
alert( );

new foo();

Why is it that the alert shows "undefined" instead of "ok"?

Answer Source

bar has gone out of scope. There is a difference between scope and context. if you want to attach a property to foo, you must attach the property to the function.

function foo() { = 'ok';
     new baz(this);

function baz(foo) {

new foo();‚Äč
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download