初涉Javascript ,会以为Javascript 是面向过程的编程语言, 和C类似,使用函数来达成效果。
<!--Add by oscar999--> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Author" CONTENT="oscar999"> <script> function test() { alert("ok"); } </script> </HEAD> <BODY> <input type=button value="Click" onclick="test()"> </BODY> </HTML>
Javascript 本身是没有类的概念,但是确确实实是面向对象的编程语言。
更多相关请参见:
原型(prototype)可以让你直接给一个变量对象添加属性或是函数。
你也可以用以下方式来定义一个对象:
<script> var jsobj = {}; jsobj.attr1 = "value1"; </script>更多相关,请参见:
<!--Add by oscar999--> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Author" CONTENT="oscar999"> <script> var module1 = (function(){ var _v1 = 0; var func1 = function(){alert("this is func1")}; var func2 = function(){alert("this is func2")}; return {"func1":func1, "func2":func2}; }()); module1.func1(); </script> </HEAD> <BODY> </BODY> </HTML>
<!--Add by oscar999--> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Author" CONTENT="oscar999"> <script> var module1 = (function(){ var _v1 = 0; var func1 = function(){alert("this is func1")}; var func2 = function(){alert("this is func2")}; return {"func1":func1, "func2":func2}; }()); var module2 = (function(mod){ mod.func3 = function(){alert("this is func3")} }(module1)); module1.func3(); </script> </HEAD> <BODY> </BODY> </HTML>看以上代码, module2 是在module1 之上进行的扩展。
<!--Add by oscar999--> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Author" CONTENT="oscar999"> <script> var module2 = (function(mod){ mod.func3 = function(){alert("this is func3")} }(module1||{})); var module1 = (function(){ var _v1 = 0; var func1 = function(){alert("this is func1")}; var func2 = function(){alert("this is func2")}; return {"func1":func1, "func2":func2}; }()); </script> </HEAD> <BODY> </BODY> </HTML>