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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:28:20+00:00 2026-06-17T07:28:20+00:00

The code snippet below is from ‘Javascript Web Applications’ by O’Reilly. In it, the

  • 0

The code snippet below is from ‘Javascript Web Applications’ by O’Reilly. In it, the author explains that using the new keyword ordinarily returns a this context, unless you specifically return something else– below, he’s returning ‘ a function that would set up a new class’, in his words (pg 7):

var Class = function(){
    var klass = function(){
        this.init.apply(this, arguments);
    };
    klass.prototype.init = function(){};
    return klass;
};

var Person = new Class;

Person.prototype.init = function(){
    // Called on Person instantiation
};

// Usage:
var person = new Person;

I’m not following this. What does returning klass do here? in the case of var Person = new Class, am I not getting a new Class object, but rather, some function that can create a Class? That’s what I’m reading from the text but it’s confusing to me.

  • 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-17T07:28:21+00:00Added an answer on June 17, 2026 at 7:28 am

    Before getting where he’s going, the first step is getting this.

    There are three things this can typically point to.
    If you have a function which is a property of an object:

    var Bob = {
        name : "Bob",
        say  : function () { console.log(this.name); }
    };
    
    Bob.say(); // "Bob"
    

    In this case, this points to whatever object owns the property (whatever is one-dot ahead, at the exact moment the function is called)

    var say = Bob.say; // passing the function to a variable
    var Sam = { name : "Sam" };
    Sam.say = Bob.say; // passing the function to an object's property
    
    say(); // undefined -- not what you were expecting
    Sam.say(); // "Sam"
    

    So this is decided at the last possible second.

    Functions also have properties of their own, like objects.
    Two of these are functions called .call and .apply and they allow you to run the function, but tell the function exactly what this is.

    Remember how say(); didn’t work on its own?

    var say = Bob.say;
    say.call(Bob); // "Bob" -- hooray!
    

    The next part of this is the one we’re most used to in object-oriented languages; using this along with new to make a new instance of a class:

    var Person = function (name) {
        this.name = name;
        this.say = function () { console.log(this.name); };
    };
    
    var bob = new Person("Bob");
    bob.say(); // "Bob"
    

    Basically, inside of this constructor function (or any function, as constructors aren’t special in JS), the function starts by checking if new was called.
    If it was, set this to a brand new object, regardless of whether it’s part of an object:

    var Creatures = {};
    Creatures.Person = Person;
    
    Creatures.Person("Bob"); // `this` === Creatures
    Creatures.name; // "Bob" -- whoops
    var bob = new Creatures.Person("Bob");
    bob.say(); // "Bob", yay!
    

    So it’s like the function is saying if (new) { this = {}; } at the top of the function.

    I said there were three possibilities.
    The third is that this === window
    If you use a function where this isn’t an object (either through call/apply, or as a property of an object) and new was not used to make a new object, then this points at window.
    That is why say(); did not work on its own, before; window.say(); is the equivalent.

    Back to new for a second — new does a couple of other things.
    Namely, it sets the value of the object’s constructor:

    var bob = new Person();
    bob instanceof Person;  // true
    

    It also gives objects made from that constructor access to the prototype object, which all instances share.

    So now, look at what’s going on inside of Class/Klass:
    We’re creating a new Class(); object.
    Inside of the Class function, this = {}; (because of new).
    Then, we’re making a new “constructor” function, klass.

    Then, we’re setting the prototyped init function, which we .apply to any new this (in the klass, at the time a new instance is called).

    Then we’re returning the klass.

    klass is actually what you’re calling when you make a new Class

    And when you call for a new object of a class, klass is being run.

    Hope that helps with new and this and .call and Class/klass.

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

Sidebar

Related Questions

I am using the below code snippet to FTP simple text files from a
I am very new to asp classic application. The below code snippet is from
Look at code snippet below from my SetupActivity - how can I test that
In the code snippet below, I'm trying to prevent the user from entering new
I have the following snippet below from my script that's using a WebRequest to
I'm using the code snippet below to get user details from a wsdl url
In the code snippet below, from my jQuery setup, I need to check if
Below is a code snippet from a book. Why can serialNumber still be set
Below is my code snippet , I want to display only 10 records from
Please see the code snippet below : #include <iostream> using namespace std; int main()

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.