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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:43:18+00:00 2026-05-27T15:43:18+00:00

Consider this code: function klass( z ) { this.a = z; return this; }

  • 0

Consider this code:

function klass( z ) {
    this.a = z;
    return this;
}

var b = klass( 5 );
var c = new klass( 9 );

When I run it in Chrome and check in the console, b turns out to be of type DOMWindow, while c is of type klass.

Although both have the property a, effectively both being an instance of klass.

  • Is using or not using new, the same?
  • Is it the same on this example but different in other situations?
  • Are there differences in efficiency or behavior?
  • 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-27T15:43:19+00:00Added an answer on May 27, 2026 at 3:43 pm

    When a function is invoked like this

    klass(6);  //called function invocation
    

    this will be set to the global object, or, if you’re in strict mode, undefined

    As a result, the first example (without the new) will return the global object with a new a property attached. In strict mode it will throw an error since this will be set to undefined, and you can’t add an a property to undefined.

    When you invoke a function with new

    new klass( 9 );  //called constructor invocation
    

    the this value is set to a new object, and is implicitly returned from the function—there’s no need to say return this

    For completeness, when you invoke a function as a method on an object:

    foo.method();  //called method invocation
    

    this will be set to the object—foo in this case.

    And when you invoke a function with apply (or call)

    method.apply(foo)  //called apply invocation 
    

    this is set to whatever you specify—foo again

    EDIT

    I mentioned strict mode in my answer. A page uses strict mode if it has

    "use strict"
    

    at the very top of it.

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

Sidebar

Related Questions

Consider this code: public <T> List<T> meth(List<?> type) { System.out.println(type); // 1 return new
Consider the following base code: (function($) { $.fn.myPlugin = function(settings) { return this.each(function() {
Consider this code... $(function() { for (var i = 0; i < 10; i++)
Consider this code below: <a href="javascript:void(-1)" id="a1">a1</a> <a href="javascript:void(-1)" id="a2">a2</a> <script type="text/javascript"> var buttons
Consider this code: function SwapColumns() { chrome.tabs.executeScript(null, {code:SwapNameAndSurname();}); } The code above is in
Consider this code example: var source = null; $.ajax({ [...], success:function(data) { source =
Please consider this code snippet: var ul = $(.list_b).find(li).remove().end(); $.each(Sites, function (index, value) {
Consider this javascript code: var bar = function () { alert(A); } var foo
Consider this code... var org = {}; org.Organization = function() { var app =
Consider this code snippet: $('#selectone').change(function(){ var amount; $.get('search.php', {search:'units'}, function(result){ //this will return only

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.