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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:13:06+00:00 2026-06-04T03:13:06+00:00

Please, can someone tell me what does this.init.apply(this, arguments) do in the code below?

  • 0

Please, can someone tell me what does this.init.apply(this, arguments) do in the code below?

I understand what apply() does in general, but in the context of the code below, what is it doing in there?

var Class = function() {

    var klass = function() {
        this.init.apply(this, arguments); //I don't really get this bit...
    };

    klass.prototype.init = function(){};

    return klass;
};

var Person = new Class;

//Usage
var someone =  new Person;

I see a lot of people using it. I’ve got an idea of what it does but can’t really put my hands on it so I need more light.

I’m going up an extra level in JS, so I wanna know everything about it, not just the simple ‘Hello world’ level.

Many thanks

  • 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-04T03:13:08+00:00Added an answer on June 4, 2026 at 3:13 am

    apply is a member function of a function object. Suppose we have:

    function saySomething(thing, anotherThing) {
        alert(this + " says " + thing + " and " + anotherThing);
    }
    

    Then we can use:

    saySomething.apply(document, ["hello", "goodbye"]);
    

    This calls the function and supplies the values of the array as arguments to the function. The first argument species the context of the function (or, what this equals when the function runs).

    You should also be aware that arguments is a special variable that holds an array of all arguments passed to the function. So, here, this.init.apply(this, arguments) means that the init function is called and passed all the of arguments that were passed to the klass constructor.

    In a real implementation, I think init would expect arguments. Consider how this would be done without apply:

    var klass = function(arg1, arg2, arg3) {
        init(arg1, arg2, arg3);
    }
    
    klass.prototype.init = function(arg1, arg2, arg3) {}
    

    If you wanted to add arg4 to init, you’d have add it in three places! By having klass transparently pass all its arguments to init, you make you code much less brittle.

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

Sidebar

Related Questions

Can someone please tell me why this code will not work? It does compile.
Can somone please tell me what does the syntax below means? public ScopeCanvas(Context context,
Can someone please tell me why the form at this location does not layout
Can someone please tell me why this GridView does not scroll smoothly even when
can someone please tell me what I'm doing wrong in this code? if($id !=
Could someone please tell me why the Iterator in this code does not return
Can someone please tell me why this doesn't work? I'm trying to show and
Can someone please tell me how I can implement the following line of pseudo-code.
can someone please tell me why this xml formatting is incorrect and why my
Please can someone tell me why this isn't working before I defenestrate everything on

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.