Thinking of the new operation of JavaScript

Source: Internet
Author: User

See an article on JavaScript single case HTTP://BLOG.CRAZYCODER.CC/POST/13

Some thoughts on javascript new operations:

Scenario 1:

function Test1 () {

return {name: ' Test1 '};

};

Result of new test1: {name: ' Test1 '}

Scenario 2:

function Test2 () {

};

Test2.prototype={name: ' Test2 '};

Result of new test2: {name: ' Test2 '}

Scenario 3:

function Test3 () {

return {name: ' Test3-1 '};

};

Test3.prototype={name: ' Test3-2 '};

Result of new test3: {name: ' Test3-1 '}

Scenario 4:

function Test4 () {

Return ' test4 ';

};

Result of new test4: {}

Scenario 5:

function Test5 () {

return [];

};

Results of new TEST5: [];

Scenario 6:

function Test6 () {

return function () {return ' Test6 '};

};

New Test6 Result: function () {return ' Test6 '};

Conclusion:

Unlike Java, JavaScript's new operation has a return value, and the return value can be an object, an array, a function, or, if the return value type does not meet the requirements, a shallow clone of the prototype is returned, otherwise the return value object is returned.

Questions:

In the original

Constructor Pointer   instance.  =Singleton;

This line of removal seems to be correct ...

Thinking of the new operation of JavaScript

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.