关于javascript中面向对象的功能:类、子类

很早以前就听说javascript是面向对象的,但一真没搞清楚到底是怎么回事,以为因为它支持DHTML  中的DOM,可以对对象进行圈套调用的原因。呆在北京也有一个多星期了,暂时没分配什么具体的任务,就自己瞎折腾看看Trakcare的使用。因为是BS开发,就涉及到了javascript. 无意中发现javascript中的function 其实就是一个类,一下子就明白了为什么说它是面向对象的了。
在网上用百度搜索了一下,类是怎么定义的算是弄明白了,但子类怎样继承父类却搜索不到相关的资料,想不到在浩翰的汉字海洋里,居然没的我要找的东东.在CSDN上发个贴子,回答的人很涌跃,却没人真正清楚。我就只好用Google了,第一个页站就是答案。.

说明问题:
1.在浩翰的汉字海洋里,我们的技术文档落后别人五年。很多的技术文档都没翻译为中文,学习稍微新一点的知识我们必须看国外的文档。
2. 以前用Google时,总觉得英文的关键词不好使。少用关键字吧,总搜出太多没用的东东,多用关键字吧,又怕自己拼的不对,一个也找不到。不像汉字,自己想怎么组合就怎么组合。其实搜文档就应该用google.


// 1. 定义类
function MyClass(para1){
 this.name=""; 
 this.attribute1="something"; //属性
 if (arguments.length>0)
  {this.init(para1);
  }
 this.DynamicMethod1; //用于外部实现的方法
 this.function1=function(para1, para2){  //方法
  alert("Function1 invoked para1=" +para1 + " para2 =" + para2);
 }
}
MyClass.prototype.ProDynamicMethod=ProtoDynamicImpl; //类的静态方法
MyClass.prototype.init=function(para1){ // 增加新的方法
 this.name=para1;
}
MyClass.prototype.toString=function(){
 return "name = " +this.name;
}
if (MyClass.prototype.invokeCount==null)
 MyClass.prototype.invokeCount==0;


//2. 定义字类
MyChildClass.prototype=new MyClass(); // 将MyClass 加在Child类的父类链上
MyChildClass.constructor=MyChildClass; //将contructor由MyClass改为自己
MyChildClass.superClass=MyClass.prototype; //加此方法是为了在子类同父类的方法冲突时,可通过superclass来调用像父
function MyChildClass(para1,para2){
 if (arguments.length>0){
  MyChildClass.superClass.init.call(this,para1);
  this.init(para2);
 }
}
MyChildClass.prototype.init=function(para1){
 this.lastName=para1;
}
MyChildClass.prototype.toString=function(){
 return MyChildClass.superClass.toString.call(this)+"  lastname=" + this.lastName;
}


//3. 调用:
function ClassInvoke()
{
myObj=new MyClass("robert");
myObj.attribute1="other something";
myObj.function1("haha","hehe");  //方法调用
myObj.DynamicMethod1=DynamicImpl; //给方法打实现
myObj.DynamicMethod1("this is showed by DynamicImpl!"); //具体调用
alert(myObj);

MyClass.prototype.invokeCount++;  //类的静态属性
MyClass.prototype.ProDynamicMethod("this is showed by prototypeDynamicImpl!")
myObj.ProDynamicMethod("myObj.ProDynamicMethod invoked!");
//其实可用prototype对类进行扩展,包括对其固有类进行扩展

myChildObj=new MyChildClass("robert2","alxandra");
alert("myChildObj.attribute1 = "+ myChildObj);
myChildObj.function1("invoked in childclass", "ok?");
}
//reference: http://www.kevlindev.com/tutorials/javascript/inheritance/

关于prototype:
Every JavaScript object has a prototype property. This property is what makes OOP possible in JavaScript, but it is a bit unusual if you come from other OO languages. Here's how it works. When you access an object property, the interpreter will look at the current object's properties to see if one by that name exists. If the name does not exist there, then the interpreter looks at the prototype property of the object to see if that object, the one pointed to by the prototype property, has the named property. If there is no property there, then the interpreter looks to see if the prototype property has a prototype property. If it does, then this process continues until either the property is found or until there are no more prototype properties to search.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值