On JavaScript Objects (II)---functions that return objects

Source: Internet
Author: User

In addition to calling the constructor using the new operator, we can also throw out the new operator and create the object with just a generic function, so that some preparatory work is performed and the object is a function of the return value. 、

function Her () {    return  {        ' Jon '    };}

Then we call her () to generate the object:

var a =//  Jon
Console.log (A.constructor);
function Object () {}

In fact, constructors can also return objects, except that the use of this value will vary.

function She () {   this. Name = ' Jon ';     return {       ' NV '    }}varnew her (); she.name;   // undefined; // ' NV '

In this case, the constructor does not return the This object that contains the property name, but instead returns the standalone object that contains the property sex. However, this is also true if the return value is an object, as a non-object, as usual.

You can understand how an object is created inside a constructor:

function Her () {   //  var this = {};    this. Name = ' Jon ';    // return this;}

On JavaScript Objects (II)---functions that return objects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.