现在的位置: 首页 > 综合 > 正文

Javascript面向对象

2011年09月23日 ⁄ 综合 ⁄ 共 5813字 ⁄ 字号 评论关闭
    //函数和类是一个概念
    function cls(){
        alert(
"hello");
    };
    
//new一个函数返回一个对象
    //new一个函数,这个函数就是所代表类的构造函数
    //其中所有代码都可以看做为了初始化一个对象而工作。
    var obj = new cls();
    alert(
typeof(obj));
    
    
//用[]引用对象的属性 方法
    //每个对象看做是多个属性 方法的集合 
    
    
//引用一个属性 方法
    //对象名.属性(方法)名  对象名["属性(方法)名"]
    function User()
    {
        
this.age = 21;
        
this.name = "wh";
    }
    
var user = new User();
    
//对象名.属性(方法)名 
    alert(user.age);
    
//对象名["属性(方法)名"]
    alert(user["name"]);
    
    
//可以动态添加删除修改属性和方法  user["sex"] ="male";
    user.sex = "male";
    user.alert 
= function(){
        alert(
"new method! alert");
    };  
    alert(user.sex);
    user.alert();
    
//删除属性
    user.name=undefined;
    
    
//定义空对象 等价于 var obj  = new Object();
    var obj = {};
    
//定义user1对象
    var user1 = {
        name:
"wh",
        age:[
"1","2","3"],
        hello:
function(){
            alert(
"hello wh!");
        }
    };
    user1.hello();
    
    
//每个函数也是一个对象 它们对应的类是Function 每一个对象都有一个子对象prototype
    //而函数也是类 prototype表示一个类成员的集合 
    //当通过new来获取一个类的对象 
    
    
//定义一个空类
    function procls()
    {}
    procls.prototype.method 
= function(){
        alert(
"procls method");
    }
    
var probj = new procls();
    
//prototype对象的成员都会成为实例化对象的成员
    probj.method();
    
    
//函数定义
    function fun1(){}
    
var fun2 = function(){};
    
var fun3 = function fun4(){};
    
var fun5 = new Function();
    
//直接在函数声明后面加括号就表示创建完成后立即进行函数调用
    var s = function(a,b){
        
return (a+b);
    }(
1,2);
    
//s是返回值 不是创建的函数
    alert(s);
    
    
//有名函数  调用之后在定义
    function f1name(){}
    
//无名函数  调用之前定义
    var f2name = function(){}
    
    
//扩展函数类型Function 添加method1方法
    //method1 对象(函数)  
    Function.prototype.method1 = function(){
        alert(
"function");
    }
    
function fun1(a,b,c)
    {
        
return a+b+c;
    }
    
//所有的函数类型都有method1方法
    fun1.method1();
    
//method1本身是一个无名函数 具有函数对象的属性方法 递归
    fun1.method1.method1();
    
    Object.prototype.getType 
= function(){
        
return typeof(this);
    }
    
//var arr1 = {};
    var arr1 = new Array();
    
function farr(a,b){
        
return a+b;
    }
    alert(arr1.getType()); 
//object
    alert(farr.getType()); //function
    
    
//函数作为参数传递或是将函数赋值给其他变量是所有事件机制的基础
    
    
//继承1 clsB在clsA的基础上增加了t1 t2 2个方法
    function clsA(){}
    
function clsB(){}
    clsB.prototype 
= clsA.prototype;
    clsB.prototype.t1
="aa";
    clsB.prototype.t2
= function(){
        alert(
"bb");
    }
    
var obj = new clsB();
    
    
    
//继承2
    function clsA(){
        
//构造函数
        this.a = 1;
    }
    
//定义clsA 的成员
    clsA.prototype = {
        m1:
function(){
            alert(
1);
        }
    }
    
    
function clsB(){
        
this.a =2;
    }
    
//clsB继承clsA
    clsB.prototype = clsA.prototype;
    clsB.prototype.m1
=function(){
        alert(
2);
    }
    
var objA = new clsA();
    
var objB = new clsB();
    
    
//构造函数输出不同
    alert(objA.a);
    alert(objB.a);
    
//输出都为2 改变了clsA的m1方法
    objA.m1();
    objB.m1();
    
    
        
//为类添加静态方法 inherit 表示继承于某类
    Function.prototype.inherit = function(baseClass){
        
for(var p in baseClass.prototype)
        {
            
this.prototype[p] = baseClass.prototype[p];
        }
    }
    
    
//继承3
    function clsA(){
        
//构造函数
    }
    
//定义clsA 的成员
    clsA.prototype = {
        m1:
function(){
            alert(
"clsA m1");
        },
        m2:
function(){
            alert(
"clsA m2");
        }
    }
    
    
function clsB(){}
    
//    for(var p in clsA.prototype)
//
    {
//
        clsB.prototype[p] = clsA.prototype[p];
//
    }

    clsB.inherit(clsA);
    clsB.prototype.m1 
= function(){
        alert(
"clsB m1")
    }
    
    
var objA = new clsA();
    
var objB = new clsB();  
    
    objA.m1();
//clsA m1
    objB.m1();//clsB m1
    
    objA.m2();
//clsA m2
    objB.m2();//clsA m2
    
    
//不能给clsB的prototype直接赋值
//
    clsB.prototype={
//
      //不能这样写
//
      m3:function(){
//
        alert("m3");
//
      }
//
    }

    clsB.prototype.name 
= "a";
    clsB.prototype.m3 
= function(){
        alert(
"m3");
    }
    alert(objB.name);
    objB.m3();

    //继承4 
    //将source的属性都赋值到destination返回destination引用
    Object.extend = function(destination, source) {
        
for (var property in source) {
            destination[property] 
= source[property];
        }
        
return destination;
    }

    
    Object.prototype.extend = function(object){
        
return Object.extend.apply(this,[this,object]);
    }
    
    
function clsA(){
        
//构造函数
    }
    
//定义clsA 的成员
    clsA.prototype = {
        a:
"1",
        m1:
function(){
            alert(
"clsA m1");
        },
        m2:
function(){
            alert(
this.a);
        }
    }
    
function clsB(){}
    clsB.prototype 
= (new clsA()).extend({
        m1:
function(){
            alert(
"clsB m1");
        },
        m2:
function(){
            alert(
"clsB m2");
        }
    });
    
    
var objA = new clsA();
    
var objB = new clsB();  
    alert(objA.a);
    alert(objB.a);
    objA.m1();
//clsA m1
    objA.m2();//clsA m2
    objB.m1();//clsB m1
    objB.m2();//clsA m2

定义类成员prototype

 //使用prototype定义类成员
 //使用function定义一个'类' 
 //函数内通过this引用的变量或方法都成为类的成员
 function clsA()
 {
    
this.age = 1;
    
this.funA();
    
 }
  
//使用函数prototype定义类新成员
  //原型对象的定义必须在创建类实例前
  clsA.prototype.funA = function(){
    alert(
this.age); 
  }
  
  
var obj = new clsA();

//this 当前运行的对象,实现对象方法时 使用this指针获得该对象自身的引用
//
this动态变化的变量 它表明了当前运行该函数的对象
//
一个对象2个方法属性的互相引用必须要通过this

//创建类2种方式
//
方法好
function clsA(){
    
this.name="wh1";
    
this.work();
}
clsA.prototype.work 
= function(){
    alert(
this.name);
}
//new的时候 创建ojbA 然后运行clsA(),并将this指针都指向objA对象
//
最后返回初始化后的对象ojbA
var ojbA = new clsA();

//方法不好
function clsB(){
    
this.name="wh1";
    
this.work=function(){
        alert(
"clsB");
    }
}
var ojbB = new clsB();
ojbB.work();

var obj1 = new Object();
var obj2 = new Object();

obj1.p =1;
obj2.p 
=2;
//定义一次 显示不同结果
obj1.getP = function(){
    alert(
this.p);
}
//this指向obj1
//
obj1.getP();
obj2.getP=obj1.getP;
//this指向obj2
obj2.getP();

//一个对象是由一个或多个属性方法组成的集合,每个集合元素不是仅能属于一个集合,
//
而是可以动态属于多个集合
//
一个方法有谁调用,this就指向谁

抱歉!评论已关闭.