counter
counter

定义 javascript对象

代码在插入的时候样子调的正好,显示的时候就乱了呢?

1.构造函数方式

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
   	function car(name,year){
		this.name=name;
		this.year=year;
		this.show=function(){
		alert("name:"+this.name+".year:"+this.year);
	     }
	}
	function show(){
		var Car=new car("红旗","2000");
		Car.show();
	}
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btn" Text="点击" runat="server" OnClientClick="show()" />
    </div>
    </form>
</body>
</html>

2.混合的构造函数/原型方式 :构造函数定义对象的所有非函数属性,原型定义对象的函数属性

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
           function car(name,year){
		this.name=name;
		this.year=year;
		this.drives=new Array("Li Lei","Wang Jun");
	    }	
            car.prototype.show=function()
            {
                alert("Name:" + this.name + ".  Year:" + this.year+".  Drives:"+this.drives);
            };
	   function shows(){
		var Car1 = new car("红旗", "2000");
                var Car2=new car("大众","2001");
                Car1.drives.push("Wu Feng");
                alert(Car1.drives);
                alert(Car2.drives);
                Car1.show();
                Car2.show();		    
	  }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btn" Text="点击" runat="server" OnClientClick="shows()" />
    </div>
    </form>
</body>
</html>

3.动态原型方法

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">   
        	function car(name,year){
			this.name=name;
			this.year=year;
			this.drives = new Array("Li Lei", "Wang Jun");
			if (typeof car._innitialized == "undefined") {
			    car.prototype.show = function () {
			        alert("Name:" + this.name + ".  Year:" + this.year + ".  Drives:" + this.drives);
			    }
			};
			}
			function shows(){
			    var Car1 = new car("红旗", "2000");
                var Car2=new car("大众","2001");
                Car1.drives.push("Wu Feng");
                alert(Car1.drives);
                alert(Car2.drives);
                Car1.show();
                Car2.show();
			    
			} 
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btn" Text="点击" runat="server" OnClientClick="shows()" />
    </div>
    </form>
</body>
</html>

4.常用第二种和第三种

5.字符串相连接

//定义数组类型的对象属性     

function StringBuffer() {

     this._string_ = new Array();

        }
//对象原型的append方法         StringBuffer.prototype.append = function (str) {             this._string_.push(str);         }
//对象原型的tosting方法         StringBuffer.prototype.tostring = function () {             return this._string_.join("");         }         function showBuffer() {             var buffer = new StringBuffer();             buffer.append("字符串1");             buffer.append("字符串2");             alert(buffer.tostring());         }
注:函数即对象
posted @ 2012-07-11 11:26  bfy  阅读(189)  评论(0编辑  收藏  举报