JS object-oriented 5-minute writing

Source: Internet
Author: User

Java code
Copy codeThe Code is as follows:
// 1st writing methods
Function Circle (r ){
This. r = r;
}
Circle. PI = 3.14159;
Circle. prototype. area = function (){
Return Circle. PI * this. r * this. r;
}
Var c = new Circle (1.0 );
Alert (c. area ());

Java code
Copy codeThe Code is as follows:
// 2nd writing methods
Var Circle = function (){
Var obj = new Object ();
Obj. PI = 3.14159;
Obj. area = function (r ){
Return this. PI * r;
}
Return obj;
}
Var c = new Circle ();
Alert (c. area (1.0 ));

Java code
Copy codeThe Code is as follows:
// 3rd writing methods
Var Circle = new Object ();
Circle. PI = 3.14159;
Circle. Area = function (r ){
Return this. PI * r;
}
Alert (Circle. Area (1.0 ));

Java code
Copy codeThe Code is as follows:
// 4th writing methods
Var Circle = {
"PI": 3.14159,
"Area": function (r ){
Return this. PI * r;
}
};
Alert (Circle. area (1.0 ));

Java code
Copy codeThe Code is as follows:
// 5th writing methods
Var Circle = new Function ("this. PI = 3.14159; this. area = function (r) {return r * this. PI ;}");
Alert (new Circle (). area (1.0 ));

Let's discuss the advantages and disadvantages of these five writing methods. Which of them is more standard, especially the last two.

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.