New Simulation Implementation in Javascript

Source: Internet
Author: User
/* Use the following Code , You can simulate the new implementation in ff and chrome * // work as a constructor to create an objectfunction constructorfn (name) {This. name = Name;} // Add a method to its prototype. note: just a function has a prototype propertyconstructorfn. prototype. calla = function () {alert ('I am calla') ;}; // Add a property to itss prototypeconstructorfn. prototype. propertyb = "I am property B"; function objectfactory () {// obtain the first parameter, essentially a function As a constructor, similar to arguments. Shift (), arguments is an array-like object. Shift removes the first element in the array and returns this element. // Question: Why can't I directly call arguments. Shift? VaR constructor = array. prototype. shift. call (arguments);/* browsers that support the _ PROTO _ attribute, such as FF or chrome, can change the corresponding prototype chain before calling. Arguments. _ PROTO _ = array. prototype; var constructor = arguments. shift (); * // the first step of New is to generate an object var OBJ ={}; // the second step of New is to change the original prototype chain of the object, so that it is equal to the prototype of the corresponding constructor. Therefore, all attributes and methods of the constructor prototype can use OBJ in the newly generated object. _ PROTO _ = (typeof constructor. prototype = 'number ')? The third step of object. prototype: constructor. Prototype; // new is to call the corresponding constructor and pass the object as the this pointer of the constructor. VaR ret = constructor. apply (OBJ, arguments); // at this time, arguments has removed the first element, that is, the following parameter section, in essence, is the constructor return (typeof ret = 'object') called ')? RET: OBJ;}; // var A = objectfactory (constructorfn, 'tenfyguo') constructed by simulating new; alert (. name);. calla (); alert (. propertyb); // var B = new constructorfn ('hello'); alert (B. name); B. calla (); alert (B. propertyb );

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.