0
function Objecte(name){

    this.name=name;

}

Objecte.prototype.look=function(){

    return function(){

        alert(this.name);

    };

}

I'm trying to access the Object's attribute but when I call the function, it alerts undefined.

extintor = new Objecte("extintor");

$(document).on('dblclick',"#extintor",extintor.look());
4
  • 1
    wrapping it and returning a function doesn't change the fact that jQuery will use .call(this,event) on it, resulting in this referencing the clicked element and not your this. Commented May 13, 2013 at 17:47
  • 1
    You wrote Objecte and not Object Commented May 13, 2013 at 17:48
  • Object has meaning in JavaScript! Commented May 13, 2013 at 17:51
  • The actual name of the Object is Objecte, that's not it. Thanks though. Commented May 13, 2013 at 17:54

3 Answers 3

1

this is not lexically defined. You need to capture its value in order to ensure that the returned function can use it.

Objecte.prototype.look=function(){
    var self = this;
    return function() {
        alert(self.name);
    };
}

You can also use $.proxy

Objecte.prototype.look=function(){
    return $.proxy(function() {
        alert(this.name);
    }, this);
}

or .bind() in modern browsers

Objecte.prototype.look=function(){
    return function() {
        alert(this.name);
    }.bind(this);
}
Sign up to request clarification or add additional context in comments.

Comments

0

The anonymous function that you return has another context of this.

Therefore you have two options:

1. Create a reference to this outside and use it inside your anonymous function

Objecte.prototype.look = function() {
    var objecteInstance = this;
    return function() {
        alert(objecteInstance.name);
    };
}

2. Use Function.prototype.bind which is supported in all major browsers including IE9+

Objecte.prototype.look = function() {
    return function() {
        alert(this.name);
    }.bind(this);
}

If you don't need to support IE8 and lower, then the second option it is - as for me it looks more elegant.

Comments

0

Does look need to return a function? I think what you are trying to do is this:

Objecte.prototype.look=function(){

    alert(this.name);

}

However, this in this context will be resolved when the function is executed and could be bound to another object. I think it is clearer in any case to use the closure declaration of objects instead of using the prototype. You could declare your object this way:

function Objecte(name) {
    var that = this;

    this.name = name;

    this.look = function() {
        alert(that.name);
    }
}

The disadvantage is that every object of the type Objecte will have its own copy of the function look, but when was the last time you ran out of memory? Another disadvantage is that you can't inherit from another object, rewrite a method and later call the original method from this object, as it will be lost. However disadvantageous that is..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.