第一个例子中在构造函数中创建的函数,很多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 并不相关,但照用不误。
- function Light() {
- this.turnOn = function () {
- alert("bingo");
- }
- }
-
- var lightA = new Light();
- var lightB = new Light();
-
- alert(lightA.turnOn === lightB.turnOn);
-
- function Dark() {
-
- }
-
- Dark.prototype.turnOn = function () {
- alert("bingo");
- }
-
- var darkA = new Dark();
- var darkB = new Dark();
- 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描述)
javascript oop模式讲解,内容丰富充实,包含dom(一个todolistshi实例)操作。
Java 第一阶段建立编程思想 【零钱通(OOP)】---- 代码 Java 第一阶段建立编程思想 【零钱通(OOP)】---- 代码 Java 第一阶段建立编程思想 【零钱通(OOP)】---- 代码 Java 第一阶段建立编程思想 【零钱通(OOP)...
oop与javascript oop与javascript oop与javascript
本文将主要涉及三种面向对象语言:Internet上最流行的语言Java,最常见的OOP语言C++,以及Borland公司用于他们的集成开发环境Delphi中的Object Pascal。这三种语言具有很多相同点。本文将从多个技术角度对这三种语言...
Javascript的OOP编程Javascript的OOP编程Javascript的OOP编程
JavaScript与OOP讲座 JavaScript与OOP讲座 JavaScript与OOP讲座
oop面向设计源代码oop面向设计源代码oop面向设计源代码oop面向设计源代码
面向对象的一个JavaScript文档,介绍js的面向对象性质 如何面向对象的 等等
当我想到JavaScript及其应用程序时,OOP并不是我想到的第一件事。 JavaScript本身并不强加一个面向对象的范例,但是它很灵活,因此您可以在认为合适的情况下应用该范例。 随着该语言将其用法扩展到Web开发之外,例如...
OOP的PPT,面向对象的代码,其中包含了Model类和上传文件等等代码
javaoop javaoop阶段项目 javaoop阶段项目 javaoop阶段项目
OOP详细代码 菜鸟们可以来观看一下哦!
javascript 是一门面向对象的语言,这里共享处理希望大家看后对大家有帮助
数据库连接类:TDB 记录集代理类: TRSProxy 页面处理类:TPage 权限控制类:TPrivilege 登录类:TLogin 配置管理类:TConfig 系统函数类:TSystem OOP之ASP应用 原创代码,技术交流,尽量勿用于商业用途,否则不...
JAVA OOP 第一章作业
JavaScript与OOP
C#OOP下载
Java OOP阶段小测试2.Java OOP阶段小测试2.