The difference between the constructor new execution and the direct execution

Source: Internet
Author: User

//Create a test constructfunctionTest () {//New executes a different point from the direct execution of this      This. Init ();};//This points to the TestTest.prototype.init =function() {Console.log (' This is Test ');}//This points to windowfunctioninit () {Console.log (' This is window ');}//Direct Executionvart = Test ();//This is windowvarTest =NewTest ();//This is Test

Add another point:
Problem with constructor return value
If the return value of a function is the data of a reference type (array, object, or function), then the function is constructed with the new operator as a constructor, and the result of the operation is replaced by its return value, when the this value in the constructor body is lost and replaced by the returned object;

//Create a test constructfunctionTest () { This. A = 10; return {}; }; Console.log (NewTest ());//Object {}//Create a test constructfunctionTest () { This. A = 10; return1; }; Console.log (NewTest ());//Test {a:10}

Reference website: http://www.jb51.net/article/47871.htm

The difference between the constructor new execution and the direct execution

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.