guanxf

我的博客:http://blog.sina.com.cn/17learning

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  71 随笔 :: 1 文章 :: 41 评论 :: 0 Trackbacks
javaScript没有集成的对象,所以采用下面三种方法模拟:

1、js原型实现继承
function Person(name,age){
  this.name=name;
  this.age=age;
}
Person.prototype.sayHello=function(){
   document.write("使用原型得到Name:"+this.name+"</br>");
}

 //var per=new Person("zhangping","21");
  //per.sayHello();


 function Student(){}
 Student.prototype=new Person("zhangping","21");
 var stu=new Student();
 Student.prototype.gade="3";
  Student.prototype.intr=function(){
     document.write(this.gade);
 }
 stu.sayHello();
stu.intr();
*/
2、构造函数实现继承
/*
function  Parent(name){
   this.name=name;
   this.sayParent=function(){
     document.write("Parent:"+this.name);
   }
}
function  Child(name,age){
  this.tempMethod=Parent;
  this.tempMethod(name);
  /*
  this.age=age;
  this.sayParent=function(){
     document.write("Child:"+this.name+"age:"+this.age);
   }
   */
   /*
}
var parent=new Parent("zhangping");
parent.sayParent();
var child=new Child("xiaoguanxianfei","11");
child.sayParent();
*/

3、使用Call Applay实现继承

function  Person(name,age,love){
    this.name=name;
    this.age=age;
    this.love=love;
    this.say=function say(){
       document.write("姓名:"+name);
    }
}
function student(name,age){
   Person.call(this,name,age);
}
function teacher(name,love){
   Person.apply(this,[name,love]);
}
var per=new Person("zhangping","21","guanxianfei");
per.say();
var stu=new student("guanxianfei","22");
stu.say();
var tea=new teacher("xiaoguanxianfei","22");
tea.say();
posted on 2011-12-15 00:55 管先飞 阅读(2161) 评论(3)  编辑  收藏

评论

# re: js面向对象---继承 2011-12-17 10:09 tb
学习了  回复  更多评论
  

# re: js面向对象---继承 2011-12-17 15:57 王鹏飞
用js自己定义一个类,在项目中还没用过。用的现成的ext提供的类  回复  更多评论
  

# re: js面向对象---继承 2011-12-20 10:12 李秋雨
谢谢分享!  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: