Five ways to create objects in Nulljavascript _javascript tips

Source: Internet
Author: User

Copy Code code as follows:

<script type= "Text/javascript" >

Method One
var p0 = new Object ();
P0.name = "XY";
P0.age = 20;
P0.sayname = function () {alert (this.name);}
P0.showageaftersomeyears = function (year) {return this.age + year};
P0.sayname ();
Alert (P0.showageaftersomeyears (10));

Method Two
function person () {
}
var p1 = new Person ();
P1.name = "XY";
P1.age = 20;
P1.sayname = function () {alert (this.name);}
P1.showageaftersomeyears = function (year) {return this.age + year};
P1.sayname ();
Alert (P1.showageaftersomeyears (10));

Method Three
function Person (name,age) {
THIS.name = name;
This.age = age;
This.sayname = function () {alert (this.name);}
This.showageaftersomeyears = function (year) {return this.age + year};
}
var p2 = new Person ("XY", 20);
P2.sayname ();
Alert (P2.showageaftersomeyears (10));

Method Four
var p3 = {
Name: "XY",
AGE:20,
Sayname:function () {alert (this.name);},
Showageaftersomeyears:function (year) {return this.age + year}
};
P3.sayname ();
Alert (P3.showageaftersomeyears (10));

Method Five
var P4 = {};
P4.name = "XY";
P4.age = 20;
P4.sayname = function () {alert (this.name);}
P4.showageaftersomeyears = function (year) {return this.age + year};
P4.sayname ();
Alert (P4.showageaftersomeyears (10));

</script>

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.