JavaScript Object-oriented basic usage

Source: Internet
Author: User

<!doctype html>
<meta charset= "Utf-8" >
<title> Untitled Document </title>
<script type= "Text/jscript" >
//js Object-oriented: Creating a Cat class
function Cat () {
}
the properties of the//js object can be added dynamically
var cat1=new Cat ();
Cat1.name= "Tom";
cat1.age=3;
cat1.color= "White";
document.write (cat1.name+ "--" +cat1.age+ "--" +cat1.color ");
Methods for customizing classes (prototype objects) in//js
document.write ("<br/>1. Factory method");
var obj=new Object ();
Obj.name= "AA";
Obj.show=function () {
}
document.write ("<br/>2. Constructors to define classes");
function person () {
}
document.write ("Everything is an object in the <br/>js");
var i=10;
document.write ("<br/>" +i.constructor);
function Person1 () {
}
var p1=new Person1 ();
document.write ("<br/>" +p1.constructor);
document.write ("<br/>" +person1.constructor);
document.write ("<br/>" +function.constructor);
document.write ("<br/>instanceof the object instance is not an object instance of a class");
if (P1 instanceof Person1) {
document.write ("<br/>p1 is an object instance of Person1");
}else{
document.write ("&LT;BR/&GT;P1 is not an object instance of Person1");
}
document.write ("Method for destroying object properties <br/>js: Delete object name. Attribute");
document.write ("<br/> attribute to object creation");
function Person2 (name) {
This.name=name;
}
var p2=new Person2 ("Tom");
document.write ("<br/>" +p2.name);
document.write ("<br/> internal function Access private variable");
function Person3 () {
Two private variables
var name= "China";
var age=10000;
This.show=function () {
document.write ("<br/>" +name+ "--" +age);
}
}
var p3=new Person3 ();
P3.show ();
document.write ("<br/>js object-oriented member function");
function Person4 (name,age) {
This.name=name;
This.age=age;
}
function speak () {
document.write ("<br/> I am:" +this.name+ "; Age:" +this.age);
}
var p4=new Person4 ("Small white", 30);
P4.fun1=speak;
P4.fun1 ();
Speak ();
/* Second Way of writing
P4.fun1 () =function speak () {}
*/
document.write ("<br/> define public functions");
function Test1 () {
This.test=function () {
document.write ("<br/> public Function");
}
}
This is an anonymous notation.
New Test1 (). Test ();
document.write ("<br/>js's Prototype Method");
Dog.prototype.shout=function () {
document.write ("Puppy barking");
}
var dog1=new Dog ();
Dog1.shout ();
</script>
<body>


</body>

qq:1327880701, learn progress together, make friends.

Copyright Notice: Bo Master original articles, reproduced please indicate the source. Http://blog.csdn.net/dzy21

JavaScript Object-oriented basic usage

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.