Constructors are classes
Object-oriented:
Inherited
Packaging
Polymorphic
Constructors are also called factory methods
Is there a new way to plant?
Each function has its own function which is extremely wasteful of resources every time new, so the function object is constantly in the new birth
This will sometimes expire.
1. When new is in front of the function
function Createperson (name,sex) {var obj=new Object (); obj.name=name; obj.sex=sex; Obj.showname=function () {alert ( THIS.name)} obj.showsex=function () {alert (this.sex);} return obj;} var p1=createperson (' Hu ', ' man ');p 1.showName ();
function Createperson (name,sex) { this.name=name; this.sex=sex; This.showname=function () {alert (this.name)} This.showsex=function () {alert (this.sex);} } var p1= new Createperson (' Hu ', ' man ');p 1.showName ();
Ptototype
Save Resources
String.prototype.trim=function ()
{
Alert (This.replace (/^\s+|\s+$/g, '));
};
var a= ' Adaf ';
A.trim ();
The default specifies that the first letter of the constructor needs to be capitalized, and in the example above, Createperson is the constructor function.
2016/4/19