`
wqy159
  • 浏览: 56001 次
  • 性别: Icon_minigender_2
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

(新增官方回复)对【无类语言的OOP(JavaScript描述) 】一贴中第一个代码段的不同意见

阅读更多

网站: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


代码





  1. function Light (light) {   

  2.     //填充对象属性   

  3.     this.light = light ? light : 0  

  4.     this.state = false  

  5.        

  6.     //对象方法。   

  7.     //放心,JavaScript 没傻到给每个对象都真去分配一个函数的地步   

  8.     this.turnOn = function () {   

  9.         this.state = true  

  10.     }   

  11. }   




回帖中也有网友提出不同意见,不过Lich_Ray给出的回复是这样的:


dennis_zane 写道

第一个例子中在构造函数中创建的函数,很多javascript的书都说这样创建的每个对象都有自己的函数版本,比如这里的Light的每个对象都有自己的turnOn,楼主说“JavaScript 没傻到给每个对象都真去分配一个函数的地步“,这一点如何证明?



没好好学编译原理或者没好好看 ECMA-262 吧?

看看下面这一段,摘自 ECMA-262 13 章的一段:


引用

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 代码




  1. function Light() {   

  2.   this.turnOn = function () {   

  3.     alert("bingo");   

  4.   }   

  5. }   

  6.   

  7. var lightA = new Light();   

  8. var lightB = new Light();   

  9.   

  10. alert(lightA.turnOn === lightB.turnOn);   

  11.   

  12. function Dark() {   

  13.   

  14. }   

  15.   

  16. Dark.prototype.turnOn = function () {   

  17.   alert("bingo");   

  18. }   

  19.   

  20. var darkA = new Dark();   

  21. var darkB = new Dark();   

  22. alert(darkA.turnOn === darkB.turnOn);  



 


在第一个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

reasonable implementation the representation of a function has two

parts, the (compiled) code and the environment.  The code is compiled

once and is constant; the environment is variable, and therefore the

function object, which references both code and environment, is

created anew for every function, and therefore occupies separate

memory for every function.



Consider your original program, but changed so that the constructor

takes an argument:



  function Light(v) { this.turnOn = function () {alert(v)} }

  var a = new Light("a");

  var b = new Light("b");



Now a.turnOn() prints "a" and b.turnOn() prints "b".  The environment

for the function that is created for "new Light("a")" contains a

variable v whose value is "a", and the environment for the function

that is created for "new Light("b")" contains a different variable v

whose value is "b", ie, it's a different environment.  But the code

for the two functions is the same, they just reference different

environments, and those environments are available as parts of the

function objects created.



There are no "stacks" here, environments in ECMAScript do not obey

stack discipline, they must (in general) be allocated on the heap and

garbage-collected.  In practice, implementations optimize this when

they can, but in your example they can't, because the two environments

that are created for the calls to "Light" live longer than the calls

do (because they are captured when the function objects are created).



(This is pretty standard stuff, so I'm guessing you haven't had much

experience with these kinds of functions before.  You may want to look

at this:

http://en.wikipedia.org/wiki/Closure_(computer_science); some of the

early examples are in ECMAScript.)



--lars



On 7/16/07, wangmiaomiao  wrote:

> Thanks a lot!

>

> But I still have a question, you say that "a.turnOn and b.turnOn do not

> contain the same function.", dose it means the two function is different in

> Memory(stack)?

>

> Looking forward to your reply! Thanks

>

> ----- Original Message -----

> From: "Lars T Hansen" <lth@acm.org>

> To:

> Cc: "TC39" <e-TC39@ecma-international.org>

> Sent: Monday, July 16, 2007 11:34 PM

> Subject: Re: FW: Can somebody take this and reply?

>

> >> Excuse me for the bothering you. But I hope somebody could help me with

> >> the

> >> following question. My code is like this: function Light() { this.turnOn

> >> =

> >> function () {alert("bingo");} } var a = new Light(); var b = new Light();

> >> alert(a.turnOn === b.turnOn); according to ur Standard ECMA-262, the two

> >> variables should use the same function, but why the compare operation

> >> will

> >> return false? Looking forward to your reply! Thanks

> >>

> >>  mio wang

> >>

> >>  Reply to:

> >

> > a.turnOn and b.turnOn do not contain the same function.  They contain

> > functions created from the same function body, but in different

> > environments (the environments in this case being created by the calls

> > to the Light constructor).

> >

> > The mechanics of this are described in section 13 of ECMA-262, where

> > you will see that the first step in evaluating an expression of the

> > form "function () { ... }" is to create a fresh function object.

> > Since === compares objects by their identities in general, two

> > function objects created by two evaluations of such a function

> > expression compares differently.

> >

> > --lars

> >

>

>


我的理解是,编译的代码确实只有一份,但是


the environment is variable, and therefore the

function object, which references both code and environment, is

created anew for every function, and therefore occupies separate

memory for every function


也就是说每个function是不同的,因为,call(调用)和实例化却是不同的,实例化是一个带有一定持久性的概念。规范关于此的陈述也仅仅是call,注意,全部是call。


不知Lich_Ray的理解如何?




《 (新增官方回复)对【无类语言的OOP(JavaScript描述) 】一贴中第一个代码段的不同意见 》 的评论也很精彩,欢迎您也添加评论。查看详细 >>

推荐相关文章:
  函数式编程语言曲高和寡?
  如何把erlang应用在项目中?




JavaEye推荐
上海乐福狗信息技术有限公司:诚聘技术经理和开发工程师
免费下载IBM社区版软件--它基于开放的标准,支持广泛的开发类型,让您的开发高效自主!
京沪穗蓉四地免费注册,SOA技术高手汇聚交锋.
上海:优秀公司德比:高薪诚聘 资深Java工程师
广州:优易公司:诚聘Java工程师,开发经理
上海:尤恩斯国际集团:诚聘开发工程师
北京:优秀公司NHNChina招聘:WEB开发,系统管理,JAVA开发, DBA


分享到:
评论

相关推荐

    无类语言的OOP(JavaScript描述)

    在无类语言中实现面向对象编程(OOP)与传统类基于的语言有所不同。这类语言通常更灵活,支持动态类型和元编程等特性。JavaScript作为一种典型的无类语言,在实现面向对象编程方面有其独特之处。 ##### 建立类 在...

    javascript oop模式讲解

    JavaScript OOP(面向对象编程)模式是编程领域中一种重要的设计思想,它允许开发者通过类和对象来组织和管理代码,提高代码的可重用性和可维护性。在这个讲解中,我们将深入探讨JavaScript中的OOP模式,特别是如何...

    Javascript(OOP).rar_javascript_javascript O_oop javascript

    JavaScript,作为一种广泛应用于Web开发的动态编程语言,其面向对象编程(Object-Oriented Programming,简称OOP)特性是理解其高级用法的关键。本文档深入探讨了JavaScript中的面向对象特性,包括类、对象、继承、...

    Java 第一阶段建立编程思想 【零钱通(OOP)】---- 代码

    Java 第一阶段建立编程思想 【零钱通(OOP)】---- 代码 Java 第一阶段建立编程思想 【零钱通(OOP)】---- 代码 Java 第一阶段建立编程思想 【零钱通(OOP)】---- 代码 Java 第一阶段建立编程思想 【零钱通(OOP)...

    OOP的PPT 面向对象的代码

    在描述中提到的"Model类"是OOP中的一个常见概念,它通常用于封装业务逻辑和数据,是软件系统中的重要组成部分。 1. 封装:这是OOP的基本原则之一,它允许我们将数据和操作数据的方法捆绑在一起,隐藏内部实现细节,...

    oop与javascript

    继承是OOP中的一个重要特性,允许创建一个新类来继承现有类的属性和方法。JavaScript通过原型链实现了继承。 ##### 3. 封装 封装是指将数据和操作这些数据的方法绑定在一起,并对外隐藏实现细节,只暴露接口供外部...

    JavaScript与OOP讲座

    6. **多态**:在JavaScript中,多态是指不同的对象对同一消息(方法调用)可以有不同的响应。这是因为JavaScript的动态类型系统允许我们在运行时改变对象的类型。 7. **鸭子类型**:JavaScript遵循“鸭子类型”原则...

    JavaOOP_第1章上机练习.zip

    在"JavaOOP_第1章上机练习.zip"这个压缩包中,我们可以预见到它包含了关于Java OOP的初步学习材料,特别是针对初学者的上机实践练习。 在Java OOP中,有四个核心概念:封装、继承、多态和抽象。这些概念是理解Java ...

    javascript的oop

    JavaScript的面向对象编程(OOP)是Web开发中不可或缺的一部分,它允许我们通过类和对象来组织和管理代码,提高代码的复用性和可维护性。这篇博客文章(链接已提供)深入探讨了JavaScript中的OOP概念,我们将在这里...

    oop 3种语言比较

    本文将主要涉及三种面向对象语言:Internet上最流行的语言Java,最常见的OOP语言C++,以及Borland公司用于他们的集成开发环境Delphi中的Object Pascal。这三种语言具有很多相同点。本文将从多个技术角度对这三种语言...

    s2 JavaOOP上机作业全部代码

    在这个“s2 JavaOOP上机作业全部代码”压缩包中,包含了学生在学习Java OOP时可能遇到的各种练习和问题的解决方案。这些代码示例旨在帮助学生深入理解类、对象、继承、封装、多态等核心OOP概念。 首先,让我们详细...

    prototype_oop_javascript_ruby_prototype_

    在JavaScript中,原型是实现OOP的一个关键机制,而在Ruby中,虽然使用的是类(Class)作为主要的OOP构造,但仍然可以找到与JavaScript原型模式相类似的思想。让我们深入了解一下这两个语言中的原型和面向对象编程。 ...

    JAVA OOP 第一章作业

    在Java编程语言中,面向对象编程(Object-Oriented Programming, 简称OOP)是一种核心的概念,它使得代码更加结构化、可维护和可重用。在"JAVA OOP 第一章作业"中,我们可以预期学生将接触到一系列基础但重要的面向...

    JavaScript与OOP

    在JavaScript中,由于函数是第一类公民,多态性主要体现在函数的动态绑定和重写上。 **JavaScript中的OOP实践** JavaScript提供了多种实现OOP的方式: 1. **构造函数与原型**:构造函数用于初始化新创建的对象,...

    清华大学oop第一讲

    清华大学的这门课程“面向对象程序设计(英)第一讲”由Hailong Yao教授,主要涵盖OOP的基本原理和C++实现。课程内容丰富,旨在提升学生的编程技能和程序设计能力,让学生能够理解和应用面向对象的方法。 首先,...

    javascript与OOP__PPT

    `apply`接受两个参数,第一个是新上下文对象,第二个是一个数组或类数组对象,用于传递给原函数的参数。与`apply`类似,`call`方法也能够改变函数调用的上下文,但参数传递方式不同,`call`直接按顺序传递参数。 **...

    Javascript的OOP编程

    JavaScript的面向对象编程(Object-Oriented Programming, OOP)是一种强大的编程范式,它允许开发者创建复杂的结构和数据模型,提高代码的可维护性和复用性。在JavaScript中,OOP主要通过构造函数、原型链、封装、...

    OOP_CODE详细代码

    在OOP_CODE详细代码中,我们可以看到一个针对初学者的教程,帮助他们理解和掌握面向对象编程的核心概念。 首先,我们要理解OOP的四个基本概念:封装、继承、多态和抽象。 1. 封装:封装是将数据和方法捆绑在一起的...

    oop面向设计源代码

    oop面向设计源代码oop面向设计源代码oop面向设计源代码oop面向设计源代码

    精通JavaScript源代码.rar

    函数式编程是JavaScript的另一个重要特性,它强调函数是“第一等公民”,可以作为参数传递、作为返回值,以及存储在变量中。高阶函数如map、filter和reduce在处理数组时非常有用。此外,闭包的概念也是JavaScript...

Global site tag (gtag.js) - Google Analytics