整理JS的prototyp,call用法深入理解

来源:互联网 发布:学网店美工 编辑:程序博客网 时间:2024/05/18 03:35
JavaScript能够实现的面向对象的特征有:
·公有属性(public field)
·公有方法(public Method)
·私有属性(private field)
·私有方法(private field)
·方法重载(method overload)
·构造函数(constructor)
·事件(event)
·单一继承(single inherit)
·子类重写父类的属性或方法(override)

·静态属性或方法(static member)


例子一(JavaScript中允许添加行为的类型):可以在类型上使用proptotype来为类型添加行为。这些行为只能在类型的实例上体现。 JS中允许的类型有Array, Boolean, Date, Enumerator, Error, Function, Number, Object, RegExp, String

Js代码
  1. <script type="text/javascript">
  2. Object.prototype.Property = 1;
  3. Object.prototype.Method = function()
  4. {
  5. alert(1);
  6. }
  7. varobj=newObject();
  8. alert(obj.Property);
  9. obj.Method();
  10. </script>

例子二(prototype使用的限制):在实例上不能使用prototype,否则发生编译错误

Js代码
  1. <script type="text/javascript">
  2. varobj=newObject();
  3. obj.prototype.Property = 1; //Error
  4. //Error
  5. obj.prototype.Method = function()
  6. {
  7. alert(1);
  8. }
  9. </script>
小结:prototype在这里相当于在类的构造方法中为某个字段赋值,格式为"类名.protype.属性名"


例子十(如何让一个类型继承于另一个类型):这个例子说明了一个类型如何从另一个类型继承。

Js代码
  1. <script type="text/javascript">
  2. functionAClass()
  3. {
  4. this.Property=1;
  5. this.Method=function()
  6. {
  7. alert(1);
  8. }
  9. }
  10. functionAClass2()
  11. {
  12. this.Property2=2;
  13. this.Method2=function()
  14. {
  15. alert(2);
  16. }
  17. }
  18. AClass2.prototype = newAClass();
  19. varobj=newAClass2();
  20. alert(obj.Property);
  21. obj.Method();
  22. alert(obj.Property2);
  23. obj.Method2();
  24. </script>
小结:prototype在这里相当于在类继承父类,格式为"类名.protype"

以上参考地址:http://blog.csdn.net/tianyitianyi1/article/details/6929916


javascript的方法可以分为三类:

a 类方法

b 对象方法

c 原型方法

例子:

function People(name)//这里相等于定义了一个People类哦
{
this.name=name;
//对象方法
this.Introduce=function(){
alert("My name is "+this.name);
}
}
//类方法
People.Run=function(){
alert("I can run");
}
//原型方法
People.prototype.IntroduceChinese=function(){
alert("我的名字是"+this.name);
}

 

//测试

var p1=new People("Windking");

p1.Introduce();

People.Run();

p1.IntroduceChinese();



function People(name){this.name=name;//对象方法this.Introduce=function(){alert("My name is "+this.name);}}//类方法People.Run=function(){alert("I can run");}//原型方法,个人认为就是构造对象时就产生的方法People.prototype.IntroduceChinese=function(){alert("我的名字是"+this.name);} <script type="text/javascript">function baseClass(){    this.showMsg = function()    {        alert("baseClass::showMsg");       }       this.baseShowMsg = function()    {        alert("baseClass::baseShowMsg");    }}baseClass.showMsg = function(){    alert("baseClass::showMsg static");}function extendClass(){    this.showMsg =function ()    {        alert("extendClass::showMsg");    }}extendClass.showMsg = function(){    alert("extendClass::showMsg static")}extendClass.prototype = new baseClass();var instance = new extendClass();instance.showMsg(); //显示extendClass::showMsginstance.baseShowMsg(); //显示baseClass::baseShowMsginstance.showMsg(); //显示extendClass::showMsgbaseClass.showMsg.call(instance);//显示baseClass::showMsg staticvar baseinstance = new baseClass();baseinstance.showMsg.call(instance);//显示baseClass::showMsg</script>//测试var p1=new People("Windking");p1.Introduce();People.Run();p1.IntroduceChinese();obj1.func.call(obj)方法意思是将obj看成obj1,调用func方法<span style="color:#FF0000;"><strong>这个理解很关键,意思是我最后肯定是执行obj1.func的方法,这点无须质疑,但是obj1会被替换成obj</strong></span>  onDblClickRow:function(rowIndex, rowData){//双击回传数据              var row_dialog = $('#damageDialog').datagrid("getSelected");      var documents = getFramesDom($('#formCode').val());                documents[0][callbackFunc].call(this);               } 通过拿到父页面的dom,然后拿到注入到dom中的函数,通过执行加强练习<script type="text/javascript">function baseClass(){    this.showMsg = function()    {        alert("baseClass::showMsg");       }       this.baseShowMsg = function()    {        alert("baseClass::baseShowMsg");    }}baseClass.showMsg = function(){    alert("baseClass::showMsg static");}function extendClass(){    this.showMsg =function ()    {        alert("extendClass::showMsg");    }}extendClass.showMsg = function(){    alert("extendClass::showMsg static")}extendClass.prototype = new baseClass();var instance = new extendClass();instance.showMsg(); //显示extendClass::showMsginstance.baseShowMsg(); //显示baseClass::baseShowMsginstance.showMsg(); //显示extendClass::showMsgbaseClass.showMsg.call(instance);//显示baseClass::showMsg staticvar baseinstance = new baseClass();baseinstance.showMsg.call(instance);//显示baseClass::showMsg</script>参考地址:http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 华为手机情景义停车事项过期怎么办 拼多多付款后商品下架了怎么办 淘宝上买化妆品买到假货了怎么办 找苹果官网解id发票丢了怎么办 客人已交订金但要取消宴席怎么办 京东买的小米电视碎屏了怎么办 京东购买的电视碎屏了怎么办 淘宝上买手机不能用不给退怎么办 天猫申请退货退款卖家不处理怎么办 在淘宝买到货到付款的假苹果怎么办 跟朋友买手机买到假货怎么办 在淘宝网上买到不合格的产品怎么办 淘宝打假师打了我的店铺怎么办 收藏品公司关门跑路员工怎么办 客户快递签收后说货物短缺怎么办 京东商城买东西商家不换货怎么办 在商场买东西过几天就降价了怎么办 天猫买东西不退货不退款怎么办 买买8p美版的怎么办 京东金条银行卡被冻结还不了怎么办 在瑞士刚买的浪琴手表不走了怎么办 刚买的手表表镜有划痕 怎么办 唯品会上买的手表有质量问题怎么办 我买的对方材料没开票给我怎么办 给对方修完车车主不给发票怎么办 买苹果手机花呗额度不够怎么办 苹果手机用别人的手机卡激活怎么办 小米商城花呗分期额度不够怎么办 淘宝已经形成订单商家不发货怎么办 小米商城退款后又想买了怎么办 淘宝退货退款后不想退了怎么办 在转转的商品被屏了怎么办 不懂如何挑选适合自己的衣服怎么办 淘宝购买商品给顾客造成损失怎么办 微信购物地址写错了怎么办 微信购物后一直不发货怎么办 微信购物不发货也不退款怎么办 淘宝退货不小心点了确认收货怎么办 外卖不小心点了确认收货怎么办 圆通快递单号查不到物流信息怎么办 在京东买东西没收到退回去了怎么办