- 浏览: 3019393 次
- 性别:
- 来自: 海外
文章分类
- 全部博客 (430)
- Programming Languages (23)
- Compiler (20)
- Virtual Machine (57)
- Garbage Collection (4)
- HotSpot VM (26)
- Mono (2)
- SSCLI Rotor (1)
- Harmony (0)
- DLR (19)
- Ruby (28)
- C# (38)
- F# (3)
- Haskell (0)
- Scheme (1)
- Regular Expression (5)
- Python (4)
- ECMAScript (2)
- JavaScript (18)
- ActionScript (7)
- Squirrel (2)
- C (6)
- C++ (10)
- D (2)
- .NET (13)
- Java (86)
- Scala (1)
- Groovy (3)
- Optimization (6)
- Data Structure and Algorithm (3)
- Books (4)
- WPF (1)
- Game Engines (7)
- 吉里吉里 (12)
- UML (1)
- Reverse Engineering (11)
- NSIS (4)
- Utilities (3)
- Design Patterns (1)
- Visual Studio (9)
- Windows 7 (3)
- x86 Assembler (1)
- Android (2)
- School Assignment / Test (6)
- Anti-virus (1)
- REST (1)
- Profiling (1)
- misc (39)
- NetOA (12)
- rant (6)
- anime (5)
- Links (12)
- CLR (7)
- GC (1)
- OpenJDK (2)
- JVM (4)
- KVM (0)
- Rhino (1)
- LINQ (2)
- JScript (0)
- Nashorn (0)
- Dalvik (1)
- DTrace (0)
- LLVM (0)
- MSIL (0)
最新评论
-
mldxs:
虽然很多还是看不懂,写的很好!
虚拟机随谈(一):解释器,树遍历解释器,基于栈与基于寄存器,大杂烩 -
HanyuKing:
Java的多维数组 -
funnyone:
Java 8的default method与method resolution -
ljs_nogard:
Xamarin workbook - .Net Core 中不 ...
LINQ的恶搞…… -
txm119161336:
allocatestlye1 顺序为 // Fields o ...
最近做的两次Java/JVM分享的概要
原文:无类语言的OOP(JavaScript描述)
本来是抱着希望想读一下别人对JavaScript的理解,不料一上来就看到了……
觉得特别郁闷,一开始就当头挨上已棒,后面也没什么心机看了。
问题就是“一个函数”是什么。从语言表象看,在我接触到的JavaScript/ActionScript引擎里,下面的代码都总是两个false:
JavaScript: (IE6, IE7, FF2, FF3)
ActionScript: (AVM2)
ECMAScript里“一个函数”本身就包含了code与environment。单纯是code的话,编译时当然能只生成一个版本;外带environment的话就没办法连environment也只保留一份。
后来发觉有人对那段做了反驳:
对【无类语言的OOP(JavaScript描述) 】一贴中第一个代码段的不同意见
嗯这篇扯得有点远。Anyway...不好说不好说。
只是,想起周末看资料的时候,看到有文章提到在语言设计中“syntax matters”。在这里的话,我是觉得发帖时的“语气也很重要”。嗯……
P.S. 参考Professional JavaScript for Web Developers中,3.5一节的内容。
我去 我以为我的理解已经够可以了 没想到你的理解能力比我还差 看看你都说了些神马
这样就两个true;
嘿嘿,这样是两个true,但原因却不是您所解释的那样。
首先是在函数字面量之后加括号的问题。这就是直接调用括号前的函数字面量,于是
等于
其次,JavaScript的函数中如果没有return,或在对某函数某次调用的控制流中没有遇到return就离开了该函数,则函数的返回值是undefined,不是false。
回到您的解释:您修改过代码后,this.turnOn就是true而不是一个函数;比较两个Light实例上的turnOn属性,自然总是相等——它们都是true而已。这与顶楼以及顶楼所引用的原帖中所讨论的不是同一个问题。
本来是抱着希望想读一下别人对JavaScript的理解,不料一上来就看到了……
引用
一. 基础:
建立类。只需声明一个函数作为类的构造函数即可。
建立类。只需声明一个函数作为类的构造函数即可。
function Light (light) { //填充对象属性 this.light = light ? light : 0 this.state = false //对象方法。 //放心,JavaScript 没傻到给每个对象都真去分配一个函数的地步 this.turnOn = function () { this.state = true } }
觉得特别郁闷,一开始就当头挨上已棒,后面也没什么心机看了。
问题就是“一个函数”是什么。从语言表象看,在我接触到的JavaScript/ActionScript引擎里,下面的代码都总是两个false:
JavaScript: (IE6, IE7, FF2, FF3)
<html> <body> <script type="text/javascript"> function Light (light) { this.light = light ? light : 0; this.state = false; this.turnOn = function () { this.state = true; } } var light1 = new Light(100); var light2 = new Light(200); document.write( light1.turnOn == light2.turnOn ); document.write( "<br />" ); document.write( light1.turnOn === light2.turnOn ); </script> </body> </html>
ActionScript: (AVM2)
function Light (light) { this.light = light ? light : 0; this.state = false; this.turnOn = function () { this.state = true; } } var light1 = new Light(100); var light2 = new Light(200); print( light1.turnOn == light2.turnOn ); print( light1.turnOn === light2.turnOn );
ECMAScript里“一个函数”本身就包含了code与environment。单纯是code的话,编译时当然能只生成一个版本;外带environment的话就没办法连environment也只保留一份。
后来发觉有人对那段做了反驳:
对【无类语言的OOP(JavaScript描述) 】一贴中第一个代码段的不同意见
嗯这篇扯得有点远。Anyway...不好说不好说。
只是,想起周末看资料的时候,看到有文章提到在语言设计中“syntax matters”。在这里的话,我是觉得发帖时的“语气也很重要”。嗯……
P.S. 参考Professional JavaScript for Web Developers中,3.5一节的内容。
评论
3 楼
黑白两相望
2011-11-28
司徒正美 写道
<pre>
function Light (light) {
this.light = light ? light : 0;
this.state = false;
this.turnOn = function () {
return this.state = true;
}()
}
var light1 = new Light(100);
var light2 = new Light(200);
document.write( light1.turnOn == light2.turnOn );
document.write( "<br />" );
document.write( light1.turnOn === light2.turnOn );
</pre>
这样就两个true;
因为
……
this.turnOn = function () {
this.state = true;
}
……
相当于
this.turnOn = function () {
this.state = true;
return false;
}
function Light (light) {
this.light = light ? light : 0;
this.state = false;
this.turnOn = function () {
return this.state = true;
}()
}
var light1 = new Light(100);
var light2 = new Light(200);
document.write( light1.turnOn == light2.turnOn );
document.write( "<br />" );
document.write( light1.turnOn === light2.turnOn );
</pre>
这样就两个true;
因为
……
this.turnOn = function () {
this.state = true;
}
……
相当于
this.turnOn = function () {
this.state = true;
return false;
}
我去 我以为我的理解已经够可以了 没想到你的理解能力比我还差 看看你都说了些神马
2 楼
RednaxelaFX
2009-05-31
司徒正美 写道
function Light(light) { this.light = light ? light : 0; this.state = false; this.turnOn = function () { return this.state = true; }() } var light1 = new Light(100); var light2 = new Light(200); document.write( light1.turnOn == light2.turnOn ); document.write( "<br />" ); document.write( light1.turnOn === light2.turnOn );
这样就两个true;
嘿嘿,这样是两个true,但原因却不是您所解释的那样。
首先是在函数字面量之后加括号的问题。这就是直接调用括号前的函数字面量,于是
a = function () { return 2 }()
等于
a = 2
其次,JavaScript的函数中如果没有return,或在对某函数某次调用的控制流中没有遇到return就离开了该函数,则函数的返回值是undefined,不是false。
a = function () { this.b = 1 }() typeof(a) // == undefined b = function (n) { if (n % 2 == 0) return true }(1) typeof(b) // == undefined
回到您的解释:您修改过代码后,this.turnOn就是true而不是一个函数;比较两个Light实例上的turnOn属性,自然总是相等——它们都是true而已。这与顶楼以及顶楼所引用的原帖中所讨论的不是同一个问题。
1 楼
司徒正美
2009-05-30
<pre>
function Light (light) {
this.light = light ? light : 0;
this.state = false;
this.turnOn = function () {
return this.state = true;
}()
}
var light1 = new Light(100);
var light2 = new Light(200);
document.write( light1.turnOn == light2.turnOn );
document.write( "<br />" );
document.write( light1.turnOn === light2.turnOn );
</pre>
这样就两个true;
因为
……
this.turnOn = function () {
this.state = true;
}
……
相当于
this.turnOn = function () {
this.state = true;
return false;
}
function Light (light) {
this.light = light ? light : 0;
this.state = false;
this.turnOn = function () {
return this.state = true;
}()
}
var light1 = new Light(100);
var light2 = new Light(200);
document.write( light1.turnOn == light2.turnOn );
document.write( "<br />" );
document.write( light1.turnOn === light2.turnOn );
</pre>
这样就两个true;
因为
……
this.turnOn = function () {
this.state = true;
}
……
相当于
this.turnOn = function () {
this.state = true;
return false;
}
发表评论
-
Function.prototype.bind
2013-09-24 18:07 0polyfill http://stackoverflow. ... -
Nashorn各种笔记
2013-07-15 17:03 0http://bits.netbeans.org/netbea ... -
JavaScript global properties are bound by name, not by value
2013-06-04 14:42 0function a() { console.log('a1' ... -
PICs and prototypes
2013-05-30 13:10 0In a lot of the implementations ... -
Nashorn notes
2013-05-28 10:44 0Nashorn NodeVisitor: the same a ... -
Building Blocks of a JavaScript Engine
2013-05-23 00:49 0sketches of my new book "B ... -
读《JavaScript语言精髓与编程实践(第二版)》
2013-05-21 00:32 02008年逛书店的时候偶 ... -
V8与c1visualizer
2011-08-21 21:00 0Subject: [v8-dev] Improved c1vi ... -
Rhino的JavaScript与Java的整合
2011-05-05 14:40 0http://www.mozilla.org/rhino/Sc ... -
this = null的杯具
2011-03-09 17:04 0this不允许赋值? FireFox表示吐槽 Chrome却欣 ... -
简单替换URL中查询字符串的参数值的代码
2010-04-11 17:49 6143刚有个需求是要把URL中query部分的一个指定参数的值替换为 ... -
JägerMonkey的组件介绍
2010-03-16 13:00 0David Mandelin JägerMonkey & ... -
眼见为实——V8的隐藏类与inline cache的例子
2010-03-02 13:42 0src/globals.h // State for inli ... -
JavaScript一些难以优化的地方
2010-03-02 08:54 0arguments with eval -
JägerMonkey与Carakan动态更新
2010-02-28 22:08 2977今天读了几篇与JavaScript引擎相关的帖。三篇关于Moz ... -
Opera的JS引擎,Carakan
2009-12-03 01:47 0Wikipedia 引用Presto is a layout ... -
with的陷阱
2009-11-21 18:41 0让eval()全局作用域执行的方法深入研究 用with(win ... -
虚拟机随谈(一):解释器,树遍历解释器,基于栈与基于寄存器,大杂烩
2009-10-17 02:52 131346(Disclaimer:如果需要转 ... -
再谈Google V8和Webkit Nitro
2009-08-21 18:11 0JavaScriptCore SquirrelFish -&g ... -
把列表里连续的数字合并到连续范围里
2009-06-07 20:46 3341论文写得快疯了嗯。上来换口气。 刚才看到问答频道的一个问题, ...
相关推荐
无类语言的OOP(JavaScript描述)
另一个指标是TypeScript的出现,TypeScript是Microsoft编写的JS的超集,并支持OOP功能:类,名称空间,模块,静态类型,泛型。 在这个简短的概述中,我将回答问题:如何,为什么以及何时在JavaScript中使用OOP? ...
面向对象编程(Object Oriented Programming,缩写为 OOP)...JavaScript 语言使用构造函数(constructor)作为对象的模板。 “构造函数”,就是专门用来生成实例对象的函数。 它就是对象的模板,描述实例对象的基本结
通过“ JavaScript高级”,学生可以深入了解JavaScript语言,例如高级语法,使用矩阵,对象,类和高级功能。 学生还将学习更复杂的概念,例如函数上下文,显式绑定,事件循环。 本课程将进一步发展他们的算法思维。 ...
它使用现代JavaScript,并使用构建(保留与纯JavaScript的兼容性),并结合了OOP(面向对象编程),FP(功能编程)和FRP(功能React编程)的元素。 在,Nest使用 ,但还与广泛的其他库(例如,从而允许轻松使用大量...
redefine.js 一个轻量级但功能强大的ES5实用程序。 过时的 是定义类的最新,最能证明未来,积极维护且...在JS世界中确实很常见的一件事,如果不熟悉ES5,那么一件事情就不那么容易解决:来自其他OOP语言JavaScript类概
我将按学期分开课堂上开发的项目第一学期编程逻辑第二学期编程语言I第三学期C中的数据结构第四学期关于人工智能I的项目Cpp中的编程语言II OOP FFT / DFT 算法设计与分析第五学期关于人工智能II的项目控制基础蓝光第...
我将自己描述为一个中级JavaScript开发人员,并且一直在尝试使用这种可爱的语言来学习尽可能多的东西。 我很快发现自己想要对我的网站尽可能有效地使用面向对象编程。 尽管对于小型站点,使用极端DRY和抽象代码的...
语言: PHP , JavaScript , C++ , Java , OOP 前端: HTML5 , CSS3 , Vue , React 后端: Laravel , WordPress , Express , Node 数据库: MySql 技术实践:敏捷,测试驱动开发 :hugging_face: 谢谢...
6.1.8 创建类模型来描述系统 85 6.2 案例研究:blackjack示例 86 6.2.1 使用CRC卡 87 6.2.2 明确blackjack类 88 6.2.3 明确类的职责 90 6.2.4 UML用例:明确协作关系 95 6.2.5 第一轮CRC卡 98 6.2.6 UML类图...
我不仅对编码充满热情,而且对使用TDD,OOP,MVC等以正确的方式进行编码充满热情。 我学习很快,并且能够相对容易地选择新语言。 我认为,良好的用户界面对于交付真正满足最终用户需求的产品至关重要。 专案 项目 ...
完整的在.net后台执行javascript脚本集合 ASP.NET 中的正则表达式 常用的匹配正则表达式和实例 经典正则表达式 delegate vs. event 我是谁?[C#] 表达式计算引擎 正式发布表达式计算引擎WfcExp V0.9(附源码) 运算...