浏览 6143 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2005-05-09
function expect(/*validate arguments as expected type*/); { for(var i=0;i<arguments.length;i+=2); { var v = arguments[i]; var type = arguments[i+1]; if(typeof v != type); { throw new Error("ArgumentTypeError");; } } } function overload(/*overload arguments*/); { if(!overload.caller.overload_done); { if(!overload.caller.overloads); { overload.caller.overloads = []; } overload.caller.overloads.push(arguments);; } } function overload_call(args, obj); { overload_call.caller.overload_done = true; for(var i=0;i<overload_call.caller.overloads.length;i++); { var compact = overload_call.caller.overloads[i]; // debugger //find matching function if(args.length == compact.length-1); { for(var j=0;j<compact.length-1;j++); { if(typeof args[j] != compact[j]); { var mismatch = true; } } if(mismatch); { mismatch = false; continue; } var fun = compact[compact.length-1]; return fun.apply(obj, args);; } } throw new Error("OverloadError");; } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2005-05-09
说明,由于javascript的动态特性,真正oo编程里头一些比如overload是不能用的,不过我们可以通过一些技巧,来给javascript增加一些overload的能力
Usage: expect()是检查参数必须符合一定条件的 比如 function add(x,y); { expect(x,"number", y, "number"); alert(x+ " + " + y + " = " +(x+y););; } //相当于强类型语言里头的 add(number x, number y)了add(3,4); //add("3", 4); //will cause error 欢迎评论 |
|
返回顶楼 | |
发表时间:2005-05-10
感谢femto 的讲解。
我对这方面很感兴趣,但找不到一个系统的讲解javascrip高级语法的资料。 大多数 javascript 资料都是讲如何操作 DHTML,怎么炫,怎么酷,很少有系统讲解js本身高级语法的。 我下载了 ECMAScript 规范,但是一条条毫无重点,看起来太费劲了。 |
|
返回顶楼 | |
发表时间:2005-05-10
列举一下我所知道的javascript和css资源把:
首先是javascript的一些书 JavaScript 2.0 - The Complete Reference (2Ed) JavaScript--The Definitive Guide 还有Java.Script.And.DHTML.Cookbook 前面两本讲javascript基础知识讲得很详细,推荐一看,第3本书是一些典型用法大全,可以用来备查. 另外我还下了JavaScript by Example和Beginning JavaScript,不过还没鉴定过,不知道好不好. 其次是css的一些书,The.CSS.Anthology.101.Essential.Tips.Tricks.and.Hacks The.Zen.of.CSS.Design.Visual.Enlightenment.for.the.Web.Feb css cookbook 另外就是网上搜出来的一些javascript和css文章了,比较零散,目前不知道有什么比较好的javascript和css 邮件列表/讨论区,有那位兄弟知道的不妨敬告一声 |
|
返回顶楼 | |
发表时间:2005-05-11
http//www.mozilla.org/rhino
rhino可以访问 java包和类 Calling overloaded methods The process of choosing a method to call based upon the types of the arguments is called overload resolution. In Java, overload resolution is performed at compile time, while in Rhino it occurs at runtime. This difference is inevitable given JavaScript’s use of dynamic typing as was discussed in Chapter 2 since the type of a variable is not known until runtime, only then can overload resolution occur. As an example, consider the following Java class that defines a number of overloaded methods and calls them. public class Overload { public String f(Object o) { return "f(Object)"; } public String f(String s) { return "f(String)"; } public String f(int i) { return "f(int)"; } public String g(String s, int i) { return "g(String,int)"; } public String g(int i, String s) { return "g(int,String)"; } public static void main(String[] args) { Overload o = new Overload(); Object[] a = new Object[] { new Integer(3), "hi", Overload.class }; for (int i=0; i < a.length; i++) System.out.println(o.f(a[i])); } } When we compile and execute the program, it produces the output f(Object) f(Object) f(Object) However, if we write a similar script var o = new Packages.Overload() var a = [ 3, "hi", Packages.Overload ]; for (var i=0; i < a.length; i++) print(o.f(a[i])); and execute it, we get the output f(int) f(String) f(Object) Because Rhino selects an overloaded method at runtime, it calls the more specific type that matches the argument. Meanwhile Java selects the overloaded method purely on the type of the argument at compile time. Although this has the benefit of selecting a method that may be a better match for each call, it does have an impact on performance since more work is done at each call. In practice this performance cost hasn’t been noticeable in real applications. Because overload resolution occurs at runtime, it can fail at runtime. For example, if we call Overload’s method g with two integers we get an error because neither form of the method is closer to the argument types than the other js> o.g(3,4) js "<stdin>", line 2 The choice of Java method Overload.g matching JavaScript argument types (number,number) is ambiguous; candidate methods are class java.lang.String g(java.lang.String,int), class java.lang.String g(int,java.lang.String) A more precise definition of overloading semantics can be found at http//www.mozilla.org/js/liveconnect/lc3_method_overloading.html. |
|
返回顶楼 | |