读《无类语言的OOP(JavaScript描述)》

原文:[url=http://www.iteye.com/topic/89554]无类语言的OOP(JavaScript描述)[/url]

本来是抱着希望想读一下别人对JavaScript的理解,不料一上来就看到了……
[quote]一. 基础:
建立类。只需声明一个函数作为类的构造函数即可。
function Light (light) {
//填充对象属性
this.light = light ? light : 0
this.state = false

//对象方法。
//放心,JavaScript 没傻到给每个对象都真去分配一个函数的地步
this.turnOn = function () {
this.state = true
}
}
[/quote]
觉得特别郁闷,一开始就当头挨上已棒,后面也没什么心机看了。

问题就是“一个函数”是什么。从语言表象看,在我接触到的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也只保留一份。

后来发觉有人对那段做了反驳:
[url=http://www.iteye.com/topic/101506]对【无类语言的OOP(JavaScript描述) 】一贴中第一个代码段的不同意见[/url]
嗯这篇扯得有点远。Anyway...不好说不好说。

只是,想起周末看资料的时候,看到有文章提到在语言设计中“syntax matters”。在这里的话,我是觉得发帖时的“语气也很重要”。嗯……

P.S. 参考Professional JavaScript for Web Developers中,3.5一节的内容。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值