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 8888707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:06:03+00:00 2026-06-14T22:06:03+00:00

Having some trouble getting ‘this’ to behave as I would expect it – Basically,

  • 0

Having some trouble getting ‘this’ to behave as I would expect it –

Basically, I have an object, and I am unable to access an array in the object from a function within the same object –

It looks like this:

var articles = {
    article : {
        1: {
            title : 'This is a Title',
            content : 'This is the content of the article'
        },
        2: {
            title : 'This is the second article',
            content : 'This is the second article content'   
        },
        3: {
            title : 'This is the third article',
            content : 'Making information transferrable. Identifiable. Manipulatable.'   
        }
    },
    numArticles : function(){
        var size = 0, key;
        for (key in this.article){
            if(this.article.hasOwnProperty(key)) size++;               
        }
        return size;
    },
    buildInterface : function(){
        var aSize = this.numArticles();
        for(var i = 0; i < aSize; i++){
            $('body').append('<article><h2>' + this.article[i].title + '</h2></article>');               
        }
    }
}

the buildInterface() function is not able to access the ‘article’ array in this scenario.

Here is an example of this in progress:

http://jsfiddle.net/merk/xV2n6/41/

Any help here would be appreciated –

I have a hunch it may be a scoping issue – hopefully it is not something related to JSFiddle –

Thanks a ton –

Peace

Mark

  • 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-06-14T22:06:04+00:00Added an answer on June 14, 2026 at 10:06 pm

    You have inconsistent indexing for your article variable: properties are defined beginning from 1, yet you start from 0 in buildArticles method for loop. You can fix this with…

    for(var i = 1; i <= aSize; i++){
      $('body').append('<article><h2>' + this.article[i].title + '</h2></article>');               
    };
    

    … or (and that’s much better for my taste, as you’re basically trying to use Object for the Array’s work) rewrite the article definition into a proper Array:

    article : [{
            title : 'This is a Title',
            content : 'This is the content of the article'
        }, {
        title : 'This is the second article',
        content : 'This is the second article content'   
        }, {
        title : 'This is the third article',
        content : 'Making information transferrable. Identifiable. Manipulatable.'   
        }],
    ...
    

    … leaving your buildArticles for loop as it is now (as indexing now properly starts from 0).

    BTW, this way you don’t even have to make a special function to count your articles: article.length would be quite enough.

    Here’s JS Fiddle with this approach’s illustration.


    As a sidenote, if you had actually checked the debugger, you would have noticed that it’s this.articles[0] that is undefined (so trying to take title out of it is wrong), and not this.articles. Hence it’s definitely not a question of scope.

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

Sidebar

Related Questions

Having some trouble getting this to work... I basically want the report to look
Having some trouble getting this right, I have a list of transaction that I
I'm having some trouble getting NH to persist my object graph. I have (something
Im having some trouble getting the syntax right. I have a movie clip that
I am having some trouble getting this simple code to work: #pragma once #include
I'm having some trouble getting personal contacts of some users I have in a
Having some trouble getting this query to work correctly. mysql_query(DELETE FROM `table` WHERE `id`
Im having some trouble getting Ajax.ActionLink to work how I expect. In /views/layout/_Layout.cshtml I
I'm having some trouble getting this layout to work correctly in IE. The problem
I'm having some trouble getting this code to compile on Linux but it works

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.