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