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

  • Home
  • SEARCH
  • 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 4013616
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:23:37+00:00 2026-05-20T09:23:37+00:00

I am using the javascript inheritance helper provided here: http://ejohn.org/blog/simple-javascript-inheritance/ I have the following

  • 0

I am using the javascript inheritance helper provided here: http://ejohn.org/blog/simple-javascript-inheritance/

I have the following code, and I have problem accessing the inherited property or function from a closure within a subclass as illustrated below. I am new to OOP javascript code and I appreciate your advice. I suppose within the closure, the context changes to JQuery (this variable) hence the problem. I appreciate your comments.

Thanks,
-A

PS – Using JQuery 1.5

     var Users = Class.extend({
                init: function(names){this.names = names;}
            });
            var HomeUsers = Users.extend({
                work:function(){ 
//                alert(this.names.length); // PRINTS A 
//                var names = this.names;  // If I make a local alias it works
                    $.map([1,2,3],function(){
                        var newName = this.names.length; //error this.names is not defined.
                        alert(newName); 
                    });

                }
            });

var users = new HomeUsers(["A"]);
users.work();
  • 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-20T09:23:38+00:00Added an answer on May 20, 2026 at 9:23 am

    this in the inner function

        function(){
            var newName = this.names.length;
            alert(newName); 
        }
    

    is not the same as this in the outer function.

    work: function(){ 
        $.map([1,2,3],function(){
            var newName = this.names.length;
            alert(newName); 
        });
    }
    

    There are many ways that people work around this:

    Store a reference to outer function’s this as another variable

    work: function(){ 
        var that = this;
        $.map([1,2,3],function(){
            var newName = that.names.length;
            alert(newName); 
        });
    }
    

    As you see, that is being used instead of this.

    Use jQuery’s $.proxy

    work: function(){ 
        $.map([1,2,3],$.proxy(function(){
            var newName = this.names.length;
            alert(newName); 
        }, this));
    }
    

    What $.proxy does is it creates another function that calls the function you passed in (in this case, the inner function), but explicitly set the context of the function (this) to the second arguments.

    Use Function.prototype.bind

    work: function(){ 
        $.map([1,2,3],function(){
            var newName = this.names.length;
            alert(newName);
        }.bind(this));
    }
    

    It works just like jQuery’s $.proxy, but in this one, you call the bind method of the function.

    It isn’t supported on all browsers, but there is a JavaScript implementation of Function.prototype.bind on MDC. You can use it.

    this in a confusing keyword, and if you want to learn more about this, then look at this.

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

Sidebar

Related Questions

I'm using this simple code: http://ejohn.org/blog/simple-javascript-inheritance/ Using this library, I made this simple class:
I'm trying to use prototype inheritance in Javascript. The problem with my code below
I've been using javascript for a while, but have never learned the language past
I've been using Javascript's Date for a project, but noticed today that my code
I'm trying a few different approaches to Javascript inheritance at the moment. I have
Without using Javascript, is there a way to make a CSS property toggle on
I'm using JavaScript to hide an image and show some text thats hidden under
I need to edit (using javascript) an SVG document embedded in an html page.
I am using JavaScript and jQuery. My main file has My.js and Ajax. My.js
Ive no problems using Javascript to read the rows of a telerik radgrid component

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.