当前位置: 首页 > news >正文

JScript中的prototype(原型)属性研究(2)

    上次的文章中我罗列了一下prototype属性在JScript中的各种用法,但是prototype这个东西却不是JScript创造出来的,JScript实际上是使用了我们设计模式中prototype pattern的一种衍生形式。下面我先简单的说一下prototype pattern,然后再来看到底JScript中的prototype是怎么回事 72_72.gif?!

    What's prototype pattern?

     Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

    用原型 实例指定创建对象的种类,并且通过 拷贝这些原型创建新的对象。

    原型模式允许一个对象再创建另外一个 可定制的对象,根本无需知道任何如何创建的细节,工作原理是:通过将一个原型对象传给那个 要发动创建的对象,这个要发动创建的对象通过请求原型对象 拷贝它们自己来实施创建。

    继续了解到底什么是prototype pattern,可以参看' 设计模式之Prototype(原型)'这篇文章,即使不懂Java也没有关系,把它的代码都当C#看就行了 emsmilep.gif

    搞清楚什么是原型了吧?反正记着一点,prototype pattern是的实现是依赖于clone这个操作的,当然要shallow copy还是deep copy的clone看自己的需要了。

    下面我们继续说JScript里的prototype,为什么我们说它和prototype pattern里的prototype不一样呢?! 这个不是我说就说出来的,也不是我吹出来的,看看这个示例,你就能大概糊涂: 
ExpandedBlockStart.gif ContractedBlock.gif < script  language ="javascript" > dot.gif
InBlock.gif
function RP()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    RP.PropertyA 
= 1;
InBlock.gif    RP.MethodA 
= function()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         alert(
"RP.MethodA ");
ExpandedSubBlockEnd.gif    }
;
InBlock.gif    
InBlock.gif    
this.PropertyA = 100
InBlock.gif    
this.MethodA = function()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         alert(
"this.MethodA");
ExpandedSubBlockEnd.gif    }
;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifRP.prototype.PropertyA 
= 10
InBlock.gifRP.prototype.MethodA 
= function()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    alert(
"RP.prototype.MethodA");
ExpandedBlockEnd.gif}
;
None.gif
</ script >

    不要着急,还没有开始做示例,只是给出了我们用来演示的一个类。RP是什么?rpwt吗?当然不是了,RP是ResearchPrototype了 emsmileo.gif。好了不废话了,看示例及结果分析。   
ExpandedBlockStart.gif ContractedBlock.gif < script  language ="javascript" > dot.gif
InBlock.gifrp 
= new RP();
InBlock.gifalert(RP.PropertyA);
InBlock.gifRP.MethodA();
InBlock.gifalert(rp.PropertyA);
ExpandedBlockEnd.gifrp.MethodA();
None.gif
</ script >

    运行结果闪亮登场:
   1
   RP.MethodA
   100
   this.MethodA

    这个 %$@#^$%&^...,不要着急,继续看哦!

ExpandedBlockStart.gif ContractedBlock.gif < script  language ="javascript" > dot.gif
InBlock.gifrp 
= new RP();
InBlock.gif
delete RP.PropertyA;
InBlock.gifalert(RP.PropertyA);
InBlock.gif
delete RP.MethodA;
InBlock.gifRP.MethodA();
InBlock.gif
delete rp.PropertyA;
InBlock.gifalert(rp.PropertyA);
InBlock.gif
delete rp.MethodA;
ExpandedBlockEnd.gifrp.MethodA();
None.gif
</ script >

    运行结果再次登场:
   undefined
   A Runtime Error has occurred.
   Do you wish to Debug?
   Line: 32
   Error: Object doesn't support this property or method
   10
   RP.prototype.MethodA


    好玩吧,看出来什么名堂了吗?这里的RP.PropertyA和RP.MethodA只是用来做参照的,可是怎么把this.PropertyA和this.MethodA都delete了,还能出来结果,而且还是prototype导入的属性和方法呢?

    这就是JScript的prototype和prototype pattern中prototype最大的不同了,JScript中的这个所谓的prototype属性其实是个语言本身支持的特性,这里没有发生任何的copy,不管shallow还是deep的。对于JScript的解释引擎,它在处理"."或"[keyName]"引用的对象的属性和方法时,先在对象本身的实例(this)中查找,如果找到就返回或执行。如果没有查找到,就查找对象的prototype(this.constructor.prototype)里是否定义了被查找的对象和方法,如果找到就返回或执行,如果没有查找到,就返回undefined(对于属性)或runtime error(对于方法)。

    正因为prototype导入类实例的属性或方法是动态查找的,所以我们才能对系统内部对象添加prototype属性和方法,比如给String对象添加trim方法:

ExpandedBlockStart.gif ContractedBlock.gif < script  lanuage ="javascript" > dot.gif
InBlock.gifString.prototype.trim()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
return this.replace(/(^\s+)|(\s+$)/g, "");
InBlock.gif}
InBlock.gif</scritp>

    显然JScript中的这种用法也是prototype pattern中的prototype不能解释和支持的。

    这下对于JScript OOP中原型继承法的理解因该没有任何的障碍了吧?同时也应该明白为什么原型继承法有那么大的天生缺陷了吧?当然如果有任何问题,欢迎继续讨论。

    附演示示例源代码:

None.gif < html >
None.gif
< head >
None.gif    <meta name="author" content="birdshome@博客园">
None.gif    
< title > JScript Prototype Research </ title >
None.gif
</ head >
None.gif
< body >
ExpandedBlockStart.gifContractedBlock.gif
< script  language ="javascript" > dot.gif
InBlock.gif
function RP()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    RP.PropertyA 
= 1;
InBlock.gif    RP.MethodA 
= function()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         alert(
"RP.MethodA ");
ExpandedSubBlockEnd.gif    }
;
InBlock.gif    
InBlock.gif    
this.PropertyA = 100
InBlock.gif    
this.MethodA = function()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         alert(
"this.MethodA");
ExpandedSubBlockEnd.gif    }
;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifRP.prototype.PropertyA 
= 10
InBlock.gifRP.prototype.MethodA 
= function()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    alert(
"RP.prototype.MethodA");
ExpandedBlockEnd.gif}
;
None.gif
</ script >
ExpandedBlockStart.gifContractedBlock.gif
< script  language ="javascript" > dot.gif
InBlock.gifrp 
= new RP();
InBlock.gif
delete RP.PropertyA;
InBlock.gifalert(RP.PropertyA);
InBlock.gif
delete RP.MethodA;
InBlock.gifRP.MethodA();
InBlock.gif
delete rp.PropertyA;
InBlock.gifalert(rp.PropertyA);
InBlock.gif
delete rp.MethodA;
ExpandedBlockEnd.gifrp.MethodA();
None.gif
</ script >
None.gif
</ body >
None.gif
</ html >
None.gif


    The End.

转载于:https://www.cnblogs.com/birdshome/archive/2005/02/21/101550.html

相关文章:

  • 今天最大的收获
  • 树形导航的实现xml
  • 加菲猫语录
  • 如何在Evolution中加密(四)
  • 采用开源软件搭建WebGIS系统(2)最简单的Demo
  • 几种调用WebService的方法
  • [Avalon] Avalon中的Conditional Formatting.
  • 转载:码根码的悲哀
  • [非技术]“西北风”新考:原来是菜和茶!
  • 配置好IIS中的SMTP服务器
  • 印象
  • 今天在做rss解析的时候碰到一个奇怪的难题
  • Microsoft Live全景图
  • 数据仓库分析图象展示--MSChart控件
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • happypack两次报错的问题
  • HTTP中GET与POST的区别 99%的错误认识
  • JavaScript的使用你知道几种?(上)
  • JavaScript学习总结——原型
  • k8s 面向应用开发者的基础命令
  • php面试题 汇集2
  • TypeScript实现数据结构(一)栈,队列,链表
  • Vue全家桶实现一个Web App
  • 回顾 Swift 多平台移植进度 #2
  • 看完九篇字体系列的文章,你还觉得我是在说字体?
  • 小程序 setData 学问多
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • 再谈express与koa的对比
  • kubernetes资源对象--ingress
  • 移动端高清、多屏适配方案
  • ​ 无限可能性的探索:Amazon Lightsail轻量应用服务器引领数字化时代创新发展
  • ​软考-高级-系统架构设计师教程(清华第2版)【第20章 系统架构设计师论文写作要点(P717~728)-思维导图】​
  • #{}和${}的区别是什么 -- java面试
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • (C语言)逆序输出字符串
  • (附源码)springboot工单管理系统 毕业设计 964158
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (九十四)函数和二维数组
  • (南京观海微电子)——COF介绍
  • (三)c52学习之旅-点亮LED灯
  • (转)人的集合论——移山之道
  • . Flume面试题
  • .NET CF命令行调试器MDbg入门(二) 设备模拟器
  • .Net CF下精确的计时器
  • .net core webapi 部署iis_一键部署VS插件:让.NET开发者更幸福
  • .NET 的程序集加载上下文
  • .NET 设计模式初探
  • .NET单元测试
  • .NET开源快速、强大、免费的电子表格组件
  • .Net通用分页类(存储过程分页版,可以选择页码的显示样式,且有中英选择)
  • @Builder用法
  • @Controller和@RestController的区别?
  • @RunWith注解作用
  • [100天算法】-每个元音包含偶数次的最长子字符串(day 53)