Posted by bianbian on 2008-03-25 01:57
本文Tags: JavaScript, prototype, this
发信人: Net (BBS上没有什么事情是bg不能解决的), 信区: WebDesign
标 题: Re: Javascript问题
发信站: 南京大学小百合站 (Tue Mar 25 01:16:03 2008)
Javascript里的this和其他OO语言不一样
Javascript其实并不适合做OO,其实OO也并不适合Javascript
扯远了。。。。。。
this你就当“执行函数的宿主”理解。因此在一串函数的执行过程中,this不停地在变。
- A = {
- member: "OK",
- method: function () {
- alert(this.member);
- }
- };
- B = A.method;
-
- A.method(); //弹出"OK". A是宿主,this.member == A.member
- B(); //弹出undefined. window(缺省)是宿主,this.member == window.member
为了解决这个问题,很多框架对此进行了包装。
比如Prototype,用bind,实际是动态生成一个function,把宿主作为动态function的执行者,这样对用户来说,this似乎没有变化。
- bind: function() {
- if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this;
- var __method = this, args = $A(arguments), object = args.shift();
- return function() {
- return __method.apply(object, args.concat($A(arguments)));
- }
- }
实际只要明白this是执行者的指针,一切都可迎刃而解。
标签:
JavaScript,
prototype,
this遵守创作共用协议,转载请链接形式注明来自
http://bianbian.org 做人要厚道
相关日志
Posted in JavaScript, Technology | No Comments »
Posted by bianbian on 2008-03-25 01:10
本文Tags: dblclick, IE, prototype, 右键
IE实在太垃圾了,再鄙视一次。。。鼠标右键双击居然没有。。。。
只好用onmouseup事件模拟一个(基于Prototype 1.6):
- if (Prototype.Browser.IE) {
- mouseCount = 0;
- Event.observe(document, 'mouseup', function(event) {
- if (Event.isRightClick(event)) {
- if (++mouseCount > 1) {
- mouseCount = 0;
- var pxy = Event.pointer(event);
- alert("right mouse double click on: " + pxy.x + ", " + pxy.y);
- } else {
- setTimeout("mouseCount = 0", 1000);
- }
- }
- });
- }
标签:
dblclick,
IE,
prototype,
右键遵守创作共用协议,转载请链接形式注明来自
http://bianbian.org 做人要厚道
相关日志
Posted in JavaScript, Technology | 1 Comment »
Posted by bianbian on 2008-03-08 08:03
本文Tags: bug, framework, JavaScript, prototype, 框架
不知道怎么给prototype提交bug report,就写在这里吧:
其实我没有测试过,只是看prototype源码的时候,觉得是个bug:
对String的原型(prototype)添加toJSON()函数只是转义了引号,未转义”\”:
- if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"';
- return "'" + escapedString.replace(/'/g, '\\\'') + "'";
如果用户在input里输入\\,转成string应该是”\\\\”,如果没有转义,再写回去的时候就丢了一个\
应该在转义引号前先转义\:
- .replace(/\\/g, '\\\\').replace(/"/g, '\\"')
标签:
bug,
framework,
JavaScript,
prototype,
框架遵守创作共用协议,转载请链接形式注明来自
http://bianbian.org 做人要厚道
相关日志
Posted in JavaScript, Technology | No Comments »