Detailed simulation of the Process Code of a jsnew object

Source: Internet
Author: User
This article describes how to simulate the process code of a jsnew object. This article describes how to simulate the process code of a js new object.

Function Person () {this. name = 1234; // return {};} Person. prototype. getname = function () {console. log (this. name);} function CreateObj () {var fn = []. shift. call (arguments); var args = arguments; var obj = new Object (); var ret = fn. apply (obj, args); obj. _ proto __= fn. prototype; return typeof ret === 'object '? Ret: obj;} var a = CreateObj (Person); console. log ();

The above code CreateObj simulates the process of an object in js new. From the code of this function, we can clearly see how an object is generated. From here, we can also find that, if the constructor returns an object, the new object is the object returned by the constructor. If the retrun data of other types is ignored by the new one.

The above is a detailed description of the Process Code for simulating a js new object. For more information, see other related articles in the first PHP community!

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.