What does Javascript's easy-to-ignore little knowledge Point new do?

Source: Internet
Author: User

Problem: usually we often write var ss = New Person (); SS is an object generated by the ' person class ', but our person method does not write return;

(var ss= person (); returns undefined because no return is written in the METHOD)

Let's start with a regular code.

function person (name) {        this. name= name;         returnthis;    };     function () {        returnthis. name;    };     var ss= New Person (a);    Console.log (ss);

The people of the Earth know that the print results are

And then the second situation

var New  function person (name) {        this. name = name;          return this ;    };     function () {        returnthis. name;    };     var ss= person.apply (obj);    Console.log (ss);

Printing results are

You might also think it's normal, so let's see

var New  function person (name) {        this. name = name;         // return this;     };     function () {        returnthis. name;    };     var ss= person.apply (obj);    Console.log (ss);

The print result is--undefined

From this we can draw the conclusion that:

1 JavaScript does not have the concept of class, it is important to remember, do not be affected by other class-based language (such as java), that the object must be created by the class, the function is a kind of object

2 New keyword We can think of doing the following several things

 var  ss = (function   var  obj = new  obj ECT (); // new an object--only to find that new is used here ... How does this explain ...? I still have a study on wood.  obj.__proto__ = person.prototype; //         The prototype of the object points to the Person's prototype  var  ret = constructor.apply (obj, arguments); //        this--point to obj  return  typeof  ret = = = ' Object '? ret:obj; //  }) (person)  

Supplementary: the object literal cannot be directly found in its own prototype, it must be delegated to its constructor, and then the constructor will find its prototype

What does Javascript's easy-to-ignore little knowledge Point new do?

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.