<div class="codetitle"> <span><a style="CURSOR: pointer" data="90510" class="copybut" id="copybut90510" onclick="doCopy('code90510')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code90510"> <br><script language="javascript" type="text/javascript"><BR> //(1)把一个方法变成一个对象,为对象创建方法和属性<BR> var Name = function () {<BR> //通过prototype给一个对象创建方法<BR> Name.prototype.Add = function (num, title) {<BR> }<BR> //也可以直接用this加方法名为对象创建方法和上面的等价<BR> this.Way = function (str) {<BR> }<BR> //为对象添加属性值<BR> Name.prototype.xing = "123";<br><br> //定义静态属性和方法<BR> Name.shi = "static";<BR> Name.Addd = function (num, title) {<BR> }<BR> //静态定义的局部属性和方法只能在静态的类型里面用<BR> alert(Name.shi);<BR> Name.Addd(1, 2);<br><br> }<BR> //方法也可以这样声明<BR> function Name1() {<BR> Name1.prototype.add = function () { }<BR> this.way = function () { }<BR> Name1.prototype.shu = "other";<BR> }<BR> //静态定义的全局属性和方法通用<BR> Name.sha = "static";<BR> Name.Addd2 = function () {<BR> }<BR> alert(Name.sha); //调用静态属性<BR> Name.Addd2(); //调用静态方法<BR> var name = new Name();<BR> name.Add(); //对象调用方法<BR> name.Way();<BR> alert(name.xing); //对象调用属性<BR> /*静态的全局变量,在方法外可以调用;静态的局部变量和方法仅限于方法内使用;实例对象不能调用静态的方法 */<BR> /*实例对象不能使用prototype; */<br><br> <BR> //(2)Javascript面向对象 继承<BR> //父类<BR> function Class() {<BR> this.name = "name";<BR> this.method = function () {<BR> alert("method");<BR> }<BR> }<BR> //子类<BR> function Class1() {<BR> this.name1 = "name1";<BR> this.method1 = function () {<BR> alert("method1");<BR> }<BR> }<BR> //子类继承父类<BR> Class1.prototype = new Class();<BR> var obj = new Class1();<BR> alert(obj.name);<BR> alert(obj.name1);<BR> obj.method();<BR> obj.method1();<BR> /****** 子类继承父类的语法,子类.prototype=new 父类(); *****/<BR> //(3)子类重写父类<BR> //子类<BR> function Class2() {<BR> this.name2 = "name2";<BR> this.method2 = function () {<BR> alert("method2");<BR> }<BR> }<BR> Class2.prototype = new Class(); //继承<BR> Class2.prototype.name = "updateName"; //重写父类的属性<BR> Class2.prototype.method = function () {//重写父类的方法<BR> alert("UpdateMethod");<BR> }<BR> var obj2 = new Class2();<BR> alert(obj2.name); //显示updateName<BR> obj2.method(); //显示UpdateMethod<BR> alert(obj2.name2);<BR> obj2.method2();<br><br> //(4){}里面的为对象<BR> var arr = new Array();<BR> arr.push({ "name": "1", "age": 12, funA: function () { } });<BR> arr.push({ "name": "2", "age": 13 });<BR> arr.push({ "name": "3", "age": 14 });<BR> for (var i = 0; i < arr.length; i++) {<BR> alert(arr[i].name);<BR> alert(arr[i].age);<BR> alert(arr[i].funA());<BR> }<BR> /****一个对象也可以这么定义***/<BR> var newObject = {<BR> "name": "Jim",<BR> "sex": "Man",<BR> Way: function () { } <BR> };<BR> </script><br> </div>