网站: JavaEye 作者: keshin 发表时间: 2007-07-16 10:28 此文章来自于 http://www.iteye.com
声明:本文系JavaEye网站原创文章,未经JavaEye网站或者作者本人书面许可,任何其他网站严禁擅自发表本文,否则必将追究法律责任!
原文链接: http://www.iteye.com/topic/101506
几天前一个同事把这篇文章发给我看,当时我看到第一个代码的时候就有不同意见,实在不吐不快,不过由于论坛要注册三天后才能发帖,所以只有等到今天才能发贴。 原文地址:http://www.iteye.com/topic/89554
代码
回帖中也有网友提出不同意见,不过Lich_Ray给出的回复是这样的:
dennis_zane 写道
第一个例子中在构造函数中创建的函数,很多javascript的书都说这样创建的每个对象都有自己的函数版本,比如这里的Light的每个对象都有自己的turnOn,楼主说“JavaScript 没傻到给每个对象都真去分配一个函数的地步“,这一点如何证明?
引用
Two uses of the FunctionBody grammar production are defined to be equated when one of the following is true:
* Both uses obtained their FunctionBody from the same location in the source text of the same ECMAScript program. This source text consists of global code and any contained function codes according to the definitions in 10.1.2. * Both uses obtained their FunctionBody from the same location in the source text of the same call to eval (15.1.2.1). This source text consists of eval code and any contained function codes according to the definitions in 10.1.2
引用
Step 1 allows an implementation to optimise the common case of a function A that has a nested function B where B is not dependent on A. In this case the implementation is allowed to reuse the same object for B instead of creating a new one every time A is called. Step 13 makes this optimisation optional; an implementation that chooses not to implement it will go to step 2.
PS: 多态对于 JavaScript 来说自动存在。看标准理解点运算符和访问运算符 [] 的行为(自动查找继承链);附件中也有一点浅显的介绍。从我写的 Mixin 代码中也能看出来:PhilipLight 不就是多个 price 属性吗?本来跟 Product 类要求的 price 并不相关,但照用不误。 事实是否如此?我们来看一段代码:
js 代码
在第一个alert处(第十行),返回的false,而在第二个alert处(22行),返回的是true。 也就是说,第一个类定义的方式,在实例化的时候js会为每个对象分配一个内存空间。而使用prototype则不会有这样的问题。 以上代码在ie和ff上面的运行结果的是一样的。 从规范看,Lich_Ray说的似乎没有问题,我没有仔细看过规范,也不是很懂什么编译原理,但是我知道对于我们实际开发中还是得以客户端的实际实现为依据 昨天我就这个问题向ecma写了封求助信,这是他们的答复(仅修改本人的email地址): This all depends somewhat on the implementation, but in a typical and 我的理解是,编译的代码确实只有一份,但是 the environment is variable, and therefore the 也就是说每个function是不同的,因为,call(调用)和实例化却是不同的,实例化是一个带有一定持久性的概念。规范关于此的陈述也仅仅是call,注意,全部是call。 不知Lich_Ray的理解如何? |
《 (新增官方回复)对【无类语言的OOP(JavaScript描述) 】一贴中第一个代码段的不同意见 》 的评论也很精彩,欢迎您也添加评论。查看详细 >>
推荐相关文章:
函数式编程语言曲高和寡?
如何把erlang应用在项目中?
JavaEye推荐
上海乐福狗信息技术有限公司:诚聘技术经理和开发工程师
免费下载IBM社区版软件--它基于开放的标准,支持广泛的开发类型,让您的开发高效自主!
京沪穗蓉四地免费注册,SOA技术高手汇聚交锋.
上海:优秀公司德比:高薪诚聘 资深Java工程师
广州:优易公司:诚聘Java工程师,开发经理
上海:尤恩斯国际集团:诚聘开发工程师
北京:优秀公司NHNChina招聘:WEB开发,系统管理,JAVA开发, DBA