Source: Internet
Author: User

Constructors are classes





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 ();


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.


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.