Javascript object-oriented syntax 03

Source: Internet
Author: User
Some basic knowledge of js indicates prototype. Each js function (class) has a prototype attribute, and the function is a class. Note that the class has prototype, but does not have common objects. Js has some common built-in classes. The following code prints the description of protytypeconsolelog (Strin js basic knowledge prototype) of built-in classes and custom classes. Each js function (class) has a prototype attribute, and the function is a class. Note that the class has prototype, but does not have common objects. Js has some common built-in classes. The following code prints the protytype console of the built-in class and custom class. log (String. prototype); console. log (Date. prototype); console. log (Object. prototype); function ClassA (name, job, born) {} console. log (ClassA. prototype); prototype generally applies to extended classes and adds some attributes or method definitions for classes. In fact, prototype is a special inherent attribute of a class. It points to a simple common object. Because it is a common object, you can add attributes or modify attributes to the object indicated by prototype. After the objects pointed to by prototype are changed, the final effect is similar to changing the class definition. Modify the object instance to which prototype points. Object. prototype. name = "111"; // Add a name attribute function ClassA (name, job, born) {} ClassA for the Object prototype. prototype. name = "123"; // you can modify the ClassA for the custom class prototype. prototype. func1 = function () {}; // You can also modify the func1 attribute of the prototype as a function pointer prototype to extend the class. For example, you can add some attributes to the prototype of a class, all objects created by the modified class have this attribute. 12345678910111213 function ClassA (name, job, born) {// at this time, the ClassA class does not define any attributes, the new object does not have the name attribute.} var objB = new ClassA (); console. log (objB. name); // output undefined Object. prototype. name = "111"; ClassA. prototype. name = "123"; // modifications to prototype take effect for new objects. var objA = new Object (); console. log (objA. name); // The prototype modification of the built-in class is also supported by the console. log (objB. name); // After prototype is modified, the prototype can be viewed as the other half of the class, and the prototype and the original Together, it is equivalent to a complete class. Function ClassA () {this. a = 100; this. B = 100;} ClassA. prototype = {'name': '000000', 'getname': function () {}}; // similar to function ClassA () {this. a = 100; this. B = 100; this. name = '000000'; this. when getName = function () {};} js accesses an object's attributes, it first searches for its own attributes. If it does not, it will then find it from the prototype of the class. Custom properties overwrite the properties defined by prototype. Function ClassA () {this. a = 100;} ClassA. prototype = {'A': 200,}; var obj = new ClassA (); console. log (obj. a); // output 100 prototype. You can also modify existing classes, such as modifying the prototype of the built-in Array class, and add a method. Array (Array) does not have the "include" method. You can expand an Array. prototype. containsObject = function (e) {for (I = 0; I

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.