<!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 ("<BR/>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