Q:

Calling an external function of nested function

Goog day.
var obj = new Foo("start");   
Foo = function(some){

            this.first = function(){
                alert("First");
                $(".clazz").click($.proxy(this.second,this));
            };

            this.second = function(){
               $(".clazz").append("<span>Second</span>");
               //this.out() // Problemb with called a method "this.out()"
            };

            this.out = function(){
                $(".clazz").append("<a>Out</a>");
                // Code
            };

            this.constructor = function(some){
                this.first();
            };
            this.constructor(some);
        };

How to call a method "this.out" from the method "this.second" ?
jsfiddle

A:

One common pattern is to explicitly declare a local variable containing a reference to the object.  This is usually called either self or _this.  The benefit is that no matter what other code does, your function will always be bound to your object.  In the below example, we see that this.prop isn't always bound correctly.  However, by only using self to refer to the object, we can avoid all issues surrounding this.
JavaScript libraries often use apply or call in a way that binds our functions in ways we don't desire.
function Foo(arg1){
    var self = this;
    self.prop = arg1;

    self.first = function(){
    };

    self.second = function(){
       alert("this.prop = " + this.out() + "\n" + // 2 (baz.prop)
             "self.prop = " + self.out() + "\n"); // 1 (bar.prop)
    };

    self.out = function(){
           return this.prop; // Depends on the context, we should use self.prop
    };
}

var bar = new Foo(1);
var baz = new Foo(2);
bar.second.apply(baz);

Here's a fiddle

