JavaScript creates objects and constructs classes to implement code _js object oriented

Source: Internet
Author: User
Create an Object
Java code
Copy Code code as follows:

<script type= "Text/javascript" >
var newobject=new Object ();
Create an Object
Newobject.firstname= "Frank";
Add a FirstName Property
Newobject.sayname=function () {
alert (this.firstname);
}
Add a Sayname method
Calling the Sayname method
Newobject.sayname ();
newobject["Sayname"] ();
var firstname=newobject["FirstName"];
var whatfunction;
if (whatvolume==1) {
Whatfunction= "Sayname";
}else if (whatvolume==2) {
whatfunction= "sayloudly"
// }
Newobject[whatfunction] ();
function sayloudly () {
Alert (This.firstName.toUpperCase ());
}
newobject.sayloudly=sayloudly;
Another way to add a method
newobject["sayloudly"] ();
</script>

Use JSON (JavaScript object notation) to create objects with the same effect as above.
Java code
Copy Code code as follows:

function sayloudly () {
Alert (This.firstName.toUpperCase ());
}
var newobject={
FirstName: "Frank",
Sayname:function () {alert (this.firstname);},
sayloudly:sayloudly
};
I can do that.
var newobject={
FirstName: "Frank",
Sayname:function () {alert (this.firstname);},
sayloudly:sayloudly,
lastname:{
LastName: "Ziggy",
Sayname:function () {alert (this.lastname);}
}
};
NewObject.lastName.sayName ();

That's OK.
Java code
Copy Code code as follows:

function sayloudly () {
Alert (This.name.toUpperCase ());
}
function Sayname () {
alert (this.name);
}
var newobject={
Name: "Frank",
Sayname:sayname,
sayloudly:sayloudly,
lastname:{
Name: "Ziggy",
Sayname:sayname
}
};
NewObject.lastName.sayName ();

Classes in JavaScript, and construction methods ...
Java code
Copy Code code as follows:

function Newclass () {
Alert ("constructor");
This.firstname= "Frank";
This.sayname=function () {alert (this.firstname);}
return this;
}
var nc=newclass ();
var nc=new newclass ();
Nc.firstname= "Ziggy"; is OK
Nc.sayname ();

You can also construct classes like this
Java code
Copy Code code as follows:

function Newclass () {
This.firstname= "Frank";
}
Newclass.prototype.sayname=function () {
alert (this.firstname);
}
var nc=new newclass ();
Nc.firstname= "Ziggy";
Nc.sayname ();
var nc2=new newclass ();
Nc2.sayname ();

Generally use prototypes to add methods so that no matter how many instances, there is only one Sayname method in memory.

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.