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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:42:31+00:00 2026-05-24T15:42:31+00:00

if I have function like this: function Apple(){ this.color = green; return this; }

  • 0

if I have function like this:

function Apple(){
    this.color = "green";
    return this;
}

When creating object like this:

var my_obj = new Apple();

is that line return this; necessary and/or is it valid by language reference?

  • 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-24T15:42:32+00:00Added an answer on May 24, 2026 at 3:42 pm

    No, returning this is not necessary, but it is valid. If the returned value is an object, new will return that object instead of the newly created object.

    See points 11.2.2 and 13.2.2 of ECMAScript 5:

    The new operator calls the internal [[Construct]] method on the constructor (usually a function):

    11.2.2 The new Operator

    The production NewExpression : new NewExpression is evaluated as follows:

    1. Let ref be the result of evaluating NewExpression.
    2. Let constructor be GetValue(ref).
    3. If Type(constructor) is not Object, throw a TypeError exception.
    4. If constructor does not implement the [[Construct]] internal method, throw a TypeError exception.
    5. Return the result of calling the [[Construct]] internal method on constructor, providing no arguments (that is, an empty list of arguments).

    The [[Construct]] internal method of functions is described in point 13.2.2:

    13.2.2 [[Construct]]

    When the [[Construct]] internal method for a Function object F is called with a possibly empty list of arguments, the following steps are taken:

    1. Let obj be a newly created native ECMAScript object.
    2. Set all the internal methods of obj as specified in 8.12.
    3. Set the [[Class]] internal property of obj to “Object”.
    4. Set the [[Extensible]] internal property of obj to true.
    5. Let proto be the value of calling the [[Get]] internal property of F with argument “prototype”.
    6. If Type(proto) is Object, set the [[Prototype]] internal property of obj to proto.
    7. If Type(proto) is not Object, set the [[Prototype]] internal property of obj to the standard built-in Object prototype
      object as described in 15.2.4.
    8. Let result be the result of calling the [[Call]] internal property of F, providing obj as the this value and providing the argument list passed into [[Construct]] as args.
    9. If Type(result) is Object then return result.
    10. Return obj.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have a function that looks something like this: //iteration over scales foreach ($surveyScales
So I have a function that looks something like this: float function(){ float x
I have a function that I use to add vectors, like this: public static
Ok, I have one JavaScript that creates rows in a table like this: function
So I have something like this: var xmlStatement:String = xmlObject.node[3].@thisValue; What mystery function do
I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
I have multiple setTimeout functions like this: function bigtomedium(visiblespan) { visiblespan.removeClass('big').addClass('medium'); setTimeout(function(){ mediumtosmall(visiblespan);},150); };
I have a string that looks like this: Name1=Value1;Name2=Value2;Name3=Value3 Is there a built-in class/function

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.