Prototype (prototype) in JavaScript

Source: Internet
Author: User

prototypeImplements the object and the object inheritance, in JS the variable, the function, almost everything is the object, but the object has the _ptoro_ attribute, this property is the commonly said prototype, is uses to point this object the prototype object, prototype object has _proto_ attribute, This is then recursively recursive until the object is traced back.
1 functionPerson (name) {2                  This. name=name;3                     };4Person.prototype.printname=function(){//by Assigning a Printname member to the prototype under the Person object, you can5Alert This. name);//instance of Person object point out Printname member6}//prototype can be stored in variables, functions, when an object Dian out of a member, the compiler will traverse through the members of the object to find the required member, if not found, will continue to search for the member under the object's prototype object until found, Otherwise, a undefault error will be returned

The following is the use of prototype to achieve the object prototype chain operation:

Replace All

3 String. prototype function (S1, S2) {4     return this. Replace (new RegExp (S1, "GM"), S2)5}//Similar to the extension method in C # , using prototype to use ReplaceAll as an extension of string, or as an inheritance, to add attributes. 
Clear Spaces
1 String. prototype function () {2     var reextraspace =/^\s* (. *?) \s+$/; 3     return this. replace (Reextraspace, "$")4 } Adds a trim method to the prototype chain of string,

Prototype (prototype) in JavaScript

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.