標籤:javascript
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>無標題文檔</title>
</head>
<script type="text/jscript">
//js物件導向:建立一個貓類
function Cat(){
}
//js對象的屬性可以動態添加
var cat1=new Cat();
cat1.name="tom";
cat1.age=3;
cat1.color="白色";
document.write(cat1.name+"--"+cat1.age+"--"+cat1.color);
//js中自訂類(原型對象)的方法
document.write("<br/>1.Factory 方法");
var obj=new Object();
obj.name="aa";
obj.show=function(){
}
document.write("<br/>2.建構函式來定義類");
function Person(){
}
document.write("<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 判斷對象執行個體是不是某個類的對象執行個體");
if(p1 instanceof Person1){
document.write("<br/>p1是Person1的對象執行個體");
}else{
document.write("<br/>p1不是Person1的對象執行個體");
}
document.write("<br/>js銷毀對象屬性的方法:delete 對象名.屬性");
document.write("<br/>給對象建立就有的屬性");
function Person2(name){
this.name=name;
}
var p2=new Person2("Tom");
document.write("<br/>"+p2.name);
document.write("<br/>內建函式訪問私人變數");
function Person3(){
//兩個私人變數
var name="China";
var age=10000;
this.show=function(){
document.write("<br/>"+name+"--"+age);
}
}
var p3=new Person3();
p3.show();
document.write("<br/>js物件導向的成員函數");
function Person4(name,age){
this.name=name;
this.age=age;
}
function speak(){
document.write("<br/>我是:"+this.name+";年齡:"+this.age);
}
var p4=new Person4("小白",30);
p4.fun1=speak;
p4.fun1();
speak();
/*第二種寫法
p4.fun1()=function speak(){}
*/
document.write("<br/>定義公用函數");
function Test1(){
this.test=function(){
document.write("<br/>公用函數");
}
}
//這裡是匿名寫法
new Test1().test();
document.write("<br/>js的原型法");
Dog.prototype.shout=function (){
document.write("小狗叫");,
}
var dog1=new Dog();
dog1.shout();
</script>
<body>
</body>
</html>
QQ:1327880701,一起學習進步,交朋友。
著作權聲明:博主原創文章,轉載請說明出處。http://blog.csdn.net/dzy21
javascript物件導向基本用法