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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:44:25+00:00 2026-05-24T17:44:25+00:00

I was reading chapter 2 of Apres Javascript Pro techniques and in particular the

  • 0

I was reading chapter 2 of Apres Javascript Pro techniques and in particular the section about Provate Methods.

The following code snippet is shown as an example:

// Listing 2-23. Example of a Private Method Only Usable by the Constructor Function
function Classroom(students, teacher) {
    // A private method for displaying all the students in the class
    function disp() {
       alert(this.names.join(", "));  // i think here there is an error. Should be alert(this.students.join(", "));
    }

    // Store the class data as public object properties
    this.students = students;
    this.teacher  = teacher;

    disp();
}

Apart the error at line 4, when i create a new Classroom object,

var class = new Classroom(["Jhon", "Bob"], "Mr. Smith");

the following error is thrown:

Uncaught TypeError: Cannot call method 'join' of undefined.

Reading at douglas.crockford.com/private.html, I found this:

By convention, we make a private that variable. This is used to make the object available to the private methods. This is a workaround for an error in the ECMAScript Language Specification which causes this to be set incorrectly for inner functions.

Indeed creating a that variable pointing to this, the previous code work as expected.

function Classroom(students, teacher) {
    var that;
    // A private method used for displaying al the students in the class
    function disp() {
        alert(that.students.join(", "));
    }

    // Store the class data as public object properties
    [...]   
    that = this;

    disp();             
}

So my question is:

  • It is always necessary to create a that variable?

If yes, this means that the example was definitively wrong.

  • 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-24T17:44:25+00:00Added an answer on May 24, 2026 at 5:44 pm

    It is only necessary to store the value of this into another variable that if you for some reason want to keep the value that this had when calling the outer method.

    The error you get (Uncaught TypeError: Cannot call method ‘join’ of undefined.) means that the property names was not found on the this object and that the value is therefore undefined and consequently cannot have a names property.

    The value of this in JavaScript is a little complicated to know. If you invoke a function f as a method, that is if you write o.f() then this is bound to o inside the function f. If you call f as a function, that is f() then this is bound to the global (window) object (!).

    Therefore, if you change the last line disp(); to this.disp();, then this will be what you expect inside disp.

    The code is indeed wrong…

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

Sidebar

Related Questions

Recently i'm reading the book Operating System Concepts Chapter VI about critical section problem,
I just finished reading the Functions chapter from Uncle Bob's Clean Code . The
I have finally finished reading Pro ASP.NET MVC 2 Framework Chapter 3: Pre-requisites but
Hi I am reading the chapter about universal hashing on CLRS. On page 234
I was reading Clojure in Action chapter 8 about TDD and experimented with the
I'm reading RESTful Web Services Cookbook and there is a complete chapter about the
I was reading about the yield keyword when I came across a sample chapter
I am reading the book JavaScript-The Good Parts, in chapter 4.14 Curry, the book
I am reading the 7th chapter of Java Concurrency in Practice. In a section
I am reading the book Javascript: The good parts. Now I am reading chapter

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.