Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Classes in JavaScript using prototype

I have a problem, I want to create a JavaScript class:

function Calculatore(txt,elements) {
    this.p= new Processor();
    this.output=txt;
    $(elements).click(this.clickHandler);   

}
Calculatore.prototype.clickHandler = function() {
var element=$(this);

// Code Here

// "this" contains the element.
// But what if I want to get the "output" var?
// I tried with Calculatore.prototype.output but no luck.

}

So how can I solve this?

like image 396
elios264 Avatar asked Oct 20 '11 22:10

elios264


People also ask

What is class prototype JavaScript?

What is prototype inheritance in JavaScript? In prototypical inheritance, prototypes are object instances to which child instances delegate undefined properties. In contrast, classes in classical inheritance are type definitions, from which child classes inherit methods and properties during instantiation.

Is JavaScript class based or prototype?

Unlike most other languages, JavaScript's object system is based on prototypes, not classes. Unfortunately, most JavaScript developers don't understand JavaScript's object system, or how to put it to best use.

What is the purpose of the prototypes in classes?

prototype. One method in memory to serve all instances is by far more efficient that every instance having their own copy of the method. Using the prototype approach to isolate all the shared methods is also very dynamic in nature.

What is prototype used for in JavaScript?

Prototypes are the mechanism by which JavaScript objects inherit features from one another. In this article, we explain what a prototype is, how prototype chains work, and how a prototype for an object can be set.


2 Answers

You can use jQuery's $.proxy:

function Calculatore(txt,elements) {
    this.p= new Processor();
    this.output=txt;
    $(elements).click($.proxy(this.clickHandler, this));
}

Calculatore.prototype.clickHandler = function(event) {
    var clickedElement = event.target;
    alert(this.output);
}

Edited. Jason brought up a good point in the comments. It's probably better to use event.target which references only the element clicked, rather than elements which may reference an array of objects matching the selection.

like image 160
hyperslug Avatar answered Sep 30 '22 08:09

hyperslug


You have a collision between this values. You currently don't have access to the instance because this has been set to the element inside a click handler.

You could make a proxy function to pass both the this value (the element) and the instance:

function Calculatore(txt,elements) {
    this.p= new Processor();
    this.output=txt;
    var inst = this; // copy instance, available as 'this' here

    $(elements).click(function(e) {
        return inst.clickHandler.call(this, e, inst); // call clickHandler with
                                                      // 'this' value and 'e'
                                                      // passed, and send 'inst'
                                                      // (the instance) as well.
                                                      // Also return the return
                                                      // value
    });

}

Calculatore.prototype.clickHandler = function(e, inst) {
    var element = $(this);

    var output = inst.output;
};
like image 40
pimvdb Avatar answered Sep 30 '22 08:09

pimvdb