学习 交流 分享

JavaScript面向对象编程-类的创建

上一篇 / 下一篇  2014-07-21 11:06:17 / 个人分类:开发相关

 

1.js的类的创建
类的创建有3种形式:对象创建法,原型创建法,函数创建法(自己命名的:))
/**Begin对象创建法*/
var Test_Default = new Object();
Test_Default.Name = "zcl";
Test_Default.Age = "23";//给自己年轻几岁
Test_Default.Sex = "男";
Test_Default.GetName = function()
{
  return this.Name
}
Test_Default.GetAge = function()
{
    return this.Age
}
Test_Default.GetSex = function()
{
    return thisSex
}
/**End**/
/**Begin原型创建法*/
function Test_Default(name, age, sex)
{
     this.Name = name;
     this.Age = age;
     this.Sex = sex;
}
Test_Default.prototype =
{
     City : "杭州",
          
     GetName : function()
     {
         return this.Name;
     },
    
     GetAge : function()
     {
         return this.Age;
     },
    
     GetSex : function()
     {
         return this.Sex;
     },
    
     GetCity : function()
     {
         return this.City;
     }
}
/**End**/
/**Begin函数创建法(个人最喜欢)*/
function Test_Default()
{
     this.Name = "zcl";
     this.Age = "23";
     this.Sex = "男";
    
     this.GetName = function()
     {
         return this.Name;
     }
    
     this.GetAge = function()
     {
         return this.Age;
     }
    
     this.GetSex = function()
     {
         return this.Sex;
     }
    
}
/**End**/
2.js的继承
//面向对象的继承原理(利用字类中的属性调用父类的对象)
function Parent()
{
     this.Name = "zcl";
    
     this.GetName = function()
     {
         alert(this.Name);
     }
}
function Child()
{
     this.Base = Parent;
     this.Base();    
}//测试
var ExtendTest = new Object();
ExtendTest.Show = function()
{
     var s = new Child();
     s.GetName();
}


TAG:

 

评分:0

我来说两句

Open Toolbar