Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6981577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:10:31+00:00 2026-05-27T18:10:31+00:00

I have a constructor function with some properties and prototypes. However, by some reason

  • 0

I have a constructor function with some properties and prototypes. However, by some reason I can’t access the properties in on of the prototype functions.

Below is some code (reduced the code so it’s readable) just to show you what I mean. In the function ‘renderTiles’ I can access ‘pairs’, but in ‘turnTile’ I can’t. Is there an obvious reason for this or is it impossible to find out with through my reduced code?

UPDATE: Updated the code to make it more clear…

DESKTOP.Game = function(){
    this.pairs = 0;
}

DESKTOP.Game.prototype.renderTiles = function() {

console.log(this.pairs); <- undefined

var gamearea = $('<div/>', {
    'text': 'testarea',
    'class': 'gamearea'
}).appendTo('.memory:last');

alert("this.rows: " + this.rows);

for (var j = 0; j < this.rows; j++){
    var box = document.createElement('div');
    for (var i = 0; i < this.cols; i++){

        var iterator = (this.cols * j) + i;

        var img = document.createElement('img');
        var aNod = document.createElement('a');

        aNod.href = "#";
        img.src = "pics/memory/0.png";

        aNod.appendChild(img);
        box.appendChild(aNod);

        var self = this;
        (function(place, index) {
            this.addEventHandler(aNod, 'click', function() { this.turnTile(place, index); return false; });
        })(iterator, this.imgArray[iterator]);
    }   
    gamearea[0].appendChild(box);
}
}

DESKTOP.Game.prototype.turnTile = function(place, id) {
    console.log(this.pairs); <- undefined

// removed code...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:10:31+00:00Added an answer on May 27, 2026 at 6:10 pm

    It’s because the value of this depends on how you call turnTiles.

    Because you’re doing:

    DESKTOP.Game.prototype.turnTile(place, index)
    

    …the value of this will be the prototype object, but pairs is placed on each individual object created from Game, not on the prototype.

    I have no idea how you’re calling renderTiles, but I’d assume that you created an instance of Game and called from there.

    Not knowing how your code works, I’d just guess that you want addEventHandler to be called on the instance as well.

    If so, you’d replace this:

    (function(place, index) {
        DESKTOP.Game.prototype.addEventHandler(aNod, 'click', function() { DESKTOP.Game.prototype.turnTile(place, index); return false; });
    })(iterator, this.imgArray[iterator]);
    

    with this:

    var self = this;
    (function(place, index) {
        self.addEventHandler(aNod, 'click', function() { self.turnTile(place, index); return false; });
    })(iterator, this.imgArray[iterator]);
    

    or something.

    Though I’m not sure why you’re using an IIFE here unless you’re in a loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: Some functions: A::A(int i_a) {cout<<int Ctor\n;} //conversion constructor void
I have the below prog Object.prototype.inherit = function(baseConstructor) { this.prototype = (baseConstructor.prototype); this.prototype.constructor =
I have this code: dojo.declare(City, null, { constructor : function(cityid, cityinfo){ } }); dojo.declare(TPolyline,
I have two constructor : function clsUsagerEmailUserName($nickName, $email) { $this->nickName = $nickName; $this->email =
I have a constructor that has a series of classes and functions within it
Suppose I have the following code: class siteMS { ... function __CONSTRUCT() { require
Background: I recently completed a tic-tac-toe game in AS3, using some simple function-based code
I have some JS you'll see below. I want the inner class object to
I have a JavaScript object those properties are static, mostly. They can be determined
I have Constructor Tree(int a, int b, int c) and second Constructor Tree(int a,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.