Skip to content Skip to sidebar Skip to footer

Calling Function From Prototype Function

I have a prototype function in javascript... I'd like to be able to call another function from that prototyped function. var objMap = new Map(500,500); var myCities = objMap.gen

Solution 1:

You are constructing the Map instance before you are assigning the prototype; and you're calling the method before you create it. Objects that are instantiated after that assignment would have a genValues method. Two fixes:

function validateValues(num){
    alert(num);
}

function Map(sizeX, sizeY) {
    this.sizeX = sizeX;
    this.sizeY = sizeY;
}
// alter the existing prototype object instead of overwriting it
Map.prototype.genValues = function (number) {
    validateValues(number);
}

// create instances only after the "class" declaration is done!
var objMap = new Map(500,500);
var myCities = objMap.genValues(5);

Post a Comment for "Calling Function From Prototype Function"