关于JavaScript,肆 (vs Python and Scheme)

JS的对象模型无疑是简单而高效的。从某种意义上来看,简直就是以函数为核心。从面向对象的角度来讲,JS是prototype-based,Python是class-based,Scheme这方面我还未学到啦~我就知道Lisp有个CLOS,还没玩过。

1. 值有类型,变量没有(dynamic typing)。JS中的变量只是名字,这和Scheme一样。Python应该也是,我不是百分百确定。

class C():
pass

D = C
c = C()
C = 1
print C # 1
del C
print D # __main__.C, though C was already deleted from context.
print c # <__main__.C instance at 0x00B44940>
print c.__class__ # __main__.C

2. JS数据类型有numbers, strings, booleans, null, undefined, objects(arrays), functions。new运算符后面可以随便跟个函数,这会创建一个新对象。在此函数内部随便干什么都行,只是这时候this引用的是刚创建的这个对象。

function f() { this.a = 2 }
f.a = 1              // f的property
alert(f)
alert(f.a)           // 1
f = new f()          // 创建对象,覆盖名字f
alert(f)             // 新对象
alert(f.a)           // 执行f()时加上的property a, 值为2

被创建的对象(objects/arrays, functions)可以随意在运行时添加properties(其内部实现应该是hashtable)。如果一个property类型为函数,并且从该对象被调用,函数中的this就是此对象:

function f() { alert(this.a) }
o = Object()
o.a = 'a in the new object'
o.method = f
o.method()             // 'a in the new object'
f2 = o.method          // f2 is equal to f, just a plain function
f2()                   // "this" refers to window object now, so "a" is not defined

如果你在全局scope定义一个函数,这个函数可被视为global object的一个方法。所以在JS中function和method真的是没什么本质区别的。 

Python的数据类型见这里:http://docs.python.org/ref/types.html

下面这段代码很能说明Python中function和method还是有区别的:

class C():
""" A dummy boring class. """

C.g = lambda self: 'method'

# unbound method
print C.g
print C.g(C())

# bound method
c = C()
print c.g
print c.g()

# changed method, still bound
c.__class__.g = lambda self: 'changed method'
print c.g
print c.g()

# function
c.g = lambda : 'function'
print c.g
print c.g()

关于JavaScript,叁

http://blog.csdn.net/g9yuayon/archive/2007/04/18/1568980.aspx

这篇贴挺有意思的。几位老兄讨论了半天,全错了。

var div = document.getElementById("testDiv");
var events = {onclick: "clicked", onchange: "changed",  onmouseover: "mouse over"}; 

for(e in events){
   div[e] 
= function(){
      alert(events[e]);
   };
}

 

这段代码根本没定义出闭包。匿名函数里访问e,但由于JavaScript没有block-level scope,e其实是全局变量,而不是局部变量。所以在这段代码执行完,e总是"onmouseover"。当事件被触发的时候,解释器尝试解析e,只找到全局的这个,所以events[e]总是"mouse over"。

嘿嘿,真有意思。 

关于JavaScript,贰 (Event)

Oh let's face it: it's fun!

The "onclick" attribute of the element (or any other element) is part of the JavaScript event model, but the "href" is NOT. Effectively, when you're writing "javascript: something" in an href, you're writing a URI. So "this" would refer to the window here. On the other hand, if you're writing in an onclick, the code you wrote will be wrapped into a handler function, in which "this" refers to the element itself.

Oh I should add that, you can also write "javascirpt:" in onXXX. in this case, "javascript:" is a legal LABEL in the JavaScript language since it appears in JS code.

About the event listener, come back to these code if anything troubles you:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Insert title here</title>
</head>

<body>
<div id="mydiv" onclick="alert('div bubble0')"><a id="mylink"
    onclick="alert('link')">link</a></div>
<script type="application/javascript">
document.getElementById('mydiv').addEventListener('click', function(e){alert('div prop1')}, true)
document.getElementById('mydiv').addEventListener('click', function(e){alert('div prop2')}, true)
document.getElementById('mydiv').addEventListener('click', function(e){alert('div bubble1')}, false)
document.getElementById('mydiv').addEventListener('click', function(e){alert('div bubble2')}, false)
</script>
</body>
</html>

关于JavaScript,壹

这些日子独立完成了系统里的一个被我们称为“select all”的复杂功能,自己很得意。本质上来说,整个过程是用一个有限状态机表示的,而这机器的状态,要使用服务器端的Java代码和客户端的JavaScript代码共同维护。我是先做出了完整的设计,然后写代码的时候一气呵成。尽管全部是自己做的,完成的时候还是惊叹这东西运行起来竟然极其精确,跟我设计的丝毫不差,完美的解决了问题。有些人说你这不是废话吗,都是你做的你还不知道。可是做软件的时候我总有这种感觉,就是,虽然知道代码会如何运行,完成的时候还是惊叹它们居然难以置信的、正确而且精准的,解决了问题,就好像问题不是自己解决的,而是计算机里的某个精灵在帮助自己一样。就像SICP的作者所说的,we conjure the spirits of the computer with our spells。

然后这两天又用零散的时间,用JavaScript实现了一个初看很困难的客户端组件。

我曾经仔细研究过JavaScript,但现在想想,当时对作用域,闭包等等没有现在理解的深入。所以这两天又再看犀牛书,这次就有针对性得多了。和当年一样,我特别讨厌网上好多人用术语唬人。我觉得你要是心里明白,不用术语就能说明白。或者你先把术语解释清楚。有好多次我都是搞明白一个术语的意思后,气不打一处来,咳,这不就是那啥那啥么,瞎忽悠什么啊,真不厚道。我决定以后每搞明白一个术语,就彻底解释一动,包括补上以前已经弄懂的一些东西。积累知识是我写博客最主要的一个目的,而且我不喜欢写技术中那些细枝末节的东西,因为这方面google远胜于我。我想表达的,是ideas & thoughts。

分页共1页 1