Sophivorus Sophivorus - 4 months ago 27
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"?


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();‚Äč