A detailed description of the construction function

Source: Internet
Author: User

//constructor Function //make your own objects replicate multiple times, and instances can access their internal properties and methods based on the level of access they set //when an object is instantiated, the constructor immediately executes any code it contains functionMyObject (msg) {//Privileged Properties (public properties)      This. mymsg = msg;//can be called only in instances that have been instantiated      This. Address = ' Shanghai '; //Private Properties     varName = ' Pride '; varAge = 29; varthat = This; //Private Methods     functionSayname () {alert (that.name); }     //Privileged methods (public methods)     //can be publicly accessed externally     //each instantiation of this method is reconstructed and prototype is a prototype share, all of which are instantiated together to refer to the same      This. Sayage =function() {alert (name);//Private members can be accessed in public methods     }     //private and privileged members within the function, each instance created by the constructor will contain the same copy of the private and privileged members .     //as a result, more instances occupy more memory } //Public Methods //applies to each instance of the object instantiated with the New keyword //adding a member to prototype will add the new method to the bottom of the constructorMyObject.prototype.sayHello =function() {alert (' Hello everyone! '); } //Static Properties //A special instance of the object, which is the constructor itself that acts as an instance of a function objectMyobject.name = ' China '; //Static MethodsMyobject.alertname =function() {alert ( This. Name); } //instantiation of varM1 =NewMyObject (' 111 '); //----Test Properties----// //Console.log (myobject.name);//china //Console.log (m1.name);//undefined, static properties do not apply to generic instances //Console.log (m1.constructor.name);//china, to access the static properties of the class, first access the instance's constructor, and then access the class's static properties //Console.log (myobject.address);//undefined, this in myObject is not the function itself, but the object that calls address, and can only be an object //Console.log (m1.address);//Shanghai At this time this refers to the instantiation of the M1  //----Test Method----// //myobject.alertname ();//china, class method for calling functions directly //m1.alertname ();//ff:m1.alertname is not a function, the Alertname is a method of the MyObject class, and the instance object has no direct relation //m1.constructor.alertname ();//china, the method (function) that invokes the object constructor (class function ) //M1.sayhello ();//hello everyone, the method under the prototype prototype of the MyObject class will be inherited by the instance //Myobject.sayhello ();//myobject.sayhello is not a Function,sayhello is a prototype method, not a method of a class  //----Test prototype----// //Console.log (M1.prototype);//undefined, the instance object has no prototype //Console.log (myobject.prototype);//object //alert (myObject.prototype.constructor);//console.log returns MyObject (msg), at which time alert () is clearer, equivalent to MyObject //Console.log (myObject.prototype.constructor.name);//china, equivalent to Myobject.name;

<script>functionCircle (RADIUS) { This. R =radius;  This. des = "Round";  This. Showinfo =function() {alert ("This is a" + This. Des); }      }         functionCircle_area (R) {returncircle.pi* This. r* This. R;} functionCircle_perimeter (R) {return2*circle.pi*R;} Circle.pi= 3.14; Circle.perimeter=Circle_perimeter; Circle.prototype.area=Circle_area; varc =NewCircle (3); //Test class Properties      //alert (CIRCLE.PI)//3.14      //alert (C.PI)//undefined because the class attribute is associated with the class itself, which is the function itself, and the class instance is not directly related.       //alert (C.CONSTRUCTOR.PI)//3.14 if you want to access a class property through a class instance, you need to first access the instance's constructor to access the class property      //alert (circle.des)//undefined because the this in the This.des in the function Circle function is not the function itself, but the object that calls R, and can only be an object.       //alert (c.des)//Circular This is now an instantiated object C.          /*Conclusion: Object-Oriented Perspective: A Class property is a direct property of a class object, and the property is not directly related to an instance object that is generated from the class object and cannot be called directly. This class property can be called directly from the class name. Property name. If you want to invoke a class property from an instance object of the class object, you can use the object instance. The constructor property invokes the object's class object and then calls its Class property through the class object JavaScript function angle: The Class property is the JavaScript function object's Straight The attribute variable (which is referred to as the attribute variable is due to the identity of the JavaScript variable and the attribute), and the attribute variable is constructed with an object reference based on the function object (an object is actually an empty object, and the constructor is saved and the The function initializes a reference to the associated properties and functions under the This keyword in the [C.prototype and constructors in this. The following related properties, functions]:) There is no direct relationship, if you want to call the constructor object's property variable pi through the constructor-generated object C,      Then you need to find the constructor object through the C.constructor property and get its property variables through the object. */        //Test class Methods     //Alert (Circle.perimeter (3));//18.4 the class method that invokes the function directly.       //Alert (C.perimeter (3));//ff:c.perimeter is not a function IE: Object or property does not support this method. Because the perimeter function is a class method of the Circle class, it is not directly related to the instance object     //Alert (C.constructor.perimeter (3));//18.84 invokes the method (function) of the object constructor (class function).       //Alert (C.area (3))//28.25 .... The area method under the prototype prototype property of the Circle class will be inherited by the instance object of the Circle class.       //Alert (Circle.area (3));//FF: Error: Circle.area is not a function because the area method is a method of the prototype property of the Circle class, not a direct method of the Circle class.          //Conclusion: Ibid, the attribute is replaced by a method, and the attribute variable is replaced by a function.             //Test prototype Object Properties      //alert (c.prototype);//undefined instance object has no Ptototype property      //alert (circle.prototype);//object Object      //alert (Circle.prototype.constructor)//return Circle function Body (function code body), equivalent to alert (circle)      //Alert (Circle.prototype.area (3)); The//nan method call succeeds, but the return result is NaN, because the THIS.R inside the area function is undefined.       //alert (Circle.prototype.PI)//undefined because the PI attribute is a direct property of the Circle class function and does not exist under the prototype property      //alert (Circle.prototype.constructor.PI)//3.14 invokes the constructor (class function) of the prototype object through the prototype object of the Circle class, and then invokes the PI property through the class function.          /*Conclusion: The prototype prototype object is an important attribute of JavaScript based on the prototype linked list implementation. JavaScript angle: 1. The instance object has no prototype property, only the constructor has the prototype property, which means that the constructor itself holds a reference to the prototype property. 2. The prototype Property object has a constructor property that holds a reference to his own constructor, which looks like a loop: A to b,b points to a ... 3.prototype object (do not be confused by my Property object, object, object property, said to be a Property object, that is, the current thing he is the first property of an object, but also an object. An object property is said to be a property of an object. The property variables and property objects will be inherited by the object that is created by the constructor that the prototype object references (function A () {} A.PROTOTYPE.PA = function () {} var oa = new A (); then OA will follow      Properties function PA). */         /*the object properties are not tested in detail here. In the process of instantiating a 1.javascript object instance through its constructor, a reference to the properties and methods referenced by all the this keywords in the constructor is saved (where the object's direct volume syntax is not discussed here). However, if the constructor does not specify through this, the object instance will not be able to invoke the method. 2.javascript You can create multiple instances through a constructor that inherits the properties and methods of the prototype object through the __proto__ property. If you read and write the properties and methods of an instance object, it does not affect the properties and methods of its prototype object, that is, the JavaScript instance object is read-only and cannot be written for the prototype object. So when we modify the properties and methods of the instance object, we can change its value.          In fact, when we try to use a property or method that inherits from the prototype object in the instance object, JavaScript copies a copy of the property or method in our instance object, so that when we manipulate it, we actually manipulate the instance object's own properties or methods. */      //Test __proto__ Properties     //alert (c.__proto__)//ff:object ie8:undefined This property points to circle.prototype, which means that calling the object returns the prototype property of the circle.       //because the __proto__ attribute is not supported for IE8 and the following versions, the following results are obtained under FF.      //alert (c.__proto__. PI)//undefined because there is no PI attribute under the function prototype, pi is the direct property of the class function Circle      //Alert (C.__proto__.area (3))//nan The function succeeds, and the return value is NaN because the THIS.R in the function body is undefined.          /*Conclusion: The __proto__ property holds a reference to the prototype property referenced by the constructor that created the object, that is, the constructor can refer to prototype, and the instance that is generated based on that constructor can also be referenced, except in a different way. */</script>

A detailed description of the construction function

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.