`

Javascript对象模型

阅读更多
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>javascript.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">    

  </head>
  	<script type="text/javascript">
  		//工厂模式创建对象
  		function showColor(){
			alert(this.color);
		}
		
		//工厂模式创建对象		
		function createCar(color){
			var tempCar = new Object();
			tempCar.color = color;
			tempCar.showColor = showColor;
			return tempCar;
		}
		
		//构造函数方式		
		function Mycar(color){
			this.color=color;
			this.showColor = function(){
				alert(this.color);
			};
		}
		
		//混合的构造函数/原型方式
		function Jeep(color){
			this.color=color;			
		}
		
		//混合的构造函数/原型方式
		Jeep.prototype.showColor = function(){
			alert(this.color);
		};
		
		//动态原型方式
		function JeepCar(color){
			this.color = color;
			//typeof JeepCar._init返回undefined
			if(typeof JeepCar._init == "undefined"){
				JeepCar.prototype.showColor = function(){
					alert(this.color);
				};
				//typeof JeepCar._init返回Boolean
				JeepCar._init = true;
				alert("create");
			}
		}
		
		//创建一个StringBuffer类处理字符串
		function StringBuffer(){
			this._string = new Array;
		}
		
		StringBuffer.prototype.append = function(date){
			this._string.push(date);
		}
		
		StringBuffer.prototype.toString = function(){
			return this._string.join("");
		}
		
		//创建新方法
		//可以给一些本地对象创建新的方法
		Number.prototype.toHexString = function(){
			return this.toString(16);//转换成16进制的
		}
		
		//创建一个方法,查找一个对角在数组中的位置
		//这样在数组就可以用这个方法了
		Array.prototype.indexOf = function(item){
			for(var i=0;i<this.length;i++){
				if(this[i]==item){
					return i;
				}
			}
			return -1;
		}
		
		//重写对象已经存在的方法
		//重写对象已经有的方法的时候可以先让原来的方法保存到另一个引用里面
		Function.prototype.toStringFor = Function.prototype.toString;
		Function.prototype.toString = function(){
			return this.toStringFor();
		}
		
  		window.onload = function(){
  			//创建Object和String对象
  			var object = new Object();  			
			var oString = new String();
			//也可以不用()直接写成new Object;
			
			//对象的废除
			oString = null;
			
			//本地对象
			//javascript的本地对象有Object,Function,Array,Date,String,Boolean
			//Number,RegExp,Error,等一些对象(还有)
			var oArray = new Array();
			oArray[0]="langhua";
			oArray[1]="boot";
			oArray[2]="text";
			//Array还有其它构造方法
			//var oArray = new Array(10);
			//var oArray = new Array("langhua","boot","text");
			//var oArray = ["langhua","boot","txt"];
			//alert(oArray.length);
			//alert(oArray.toString());//output langhua,boot,text
			//alert(oArray.valueOf());//output langhua,boot,text
			//Array的方法 join()返回String字符串
			alert(oArray.join(".."));//output langhua..boot..text
			//String也可以用split("分割正则表达示")返回一个数组
			//也可以直接split("")这样出来的就是每个字符
			//concat()方法 连结字符串,也可以连结数组
			var newArray = oArray.concat(oArray);
			//alert(newArray); output langhua,boot.text,langhua,boot,text
			
			//slice()方法
			
			var array_1 = newArray.slice(1);	//从数组的第一位到最后一件(数组的首位是0)
			var array_2 = newArray.slice(1,3);	//从数组的第一位到第二位,左闭右开(数组的首位是0)
			//alert(array_1); output boot.text,langhua,boot,text
			//alert(array_2); output boot.text
			
			//push()和pop()方法模仿Stack
			var stack = new Array;
			stack.push("langhua1");
			stack.push("langhua2");
			stack.push("langhua3");
			
			//alert(stack);
			var item = stack.pop();
			//alert(stack);
			
			//push()和shift()方法模仿队列 shift()删数数组下标为0的元素,并使下标为1的元素下标为0
			stack.shift();
			//alert(stack);
			//unshift("");
			stack.unshift("unshift"); //使数组下标为0的元素改变为传过来的值
			//alert(stack);
			//reverse()对数组进行反转,sort()对数进行排序
			var date = new Date();
			//alert(date);
			
			//内置对象
			//Math,global对象
			//global有很多方法,如isNaN(),isFinite,parseInt(),parseFloat();
			//encodeURI(),encodeURIComponent();decodeURI(),decodeURIComponent();eval();
			//alert(eval(1+2+3));
			//宿主对象
			
			//工厂模式创建对象
			//但是工厂模式的方法要写在外面,这样就不像对象了
			var car = createCar("red");
			car.showColor();
			
			//构造函数方式
			//这种方式会重复才生函数
			var mycar = new Mycar("blue");//注意是new来创建
			mycar.showColor();
			
			//混合的构造函数/原型方式
			//这种方式书上说非常的好,就用这样比较好...
			//书上说好就好吧,我没有意见的
			var myjeep_1 = new Jeep("blank1");
			myjeep_1.showColor();
			alert(myjeep_1.color);
			
			var myjeep_2 = new Jeep("blank2");
			myjeep_2.showColor();
			alert(myjeep_2.color);
			
			//还有一种用叫动态原型方式
			var JeepCars1 = new JeepCar("Jeepcar1");
			JeepCars1.showColor();
			var JeepCars2 = new JeepCar("Jeepcar2");			
			JeepCars2.showColor();
			var buff = new StringBuffer();
			buff.append("la");
			buff.append("ha");
			alert(buff.toString());
			
			//本地对象的新方法的使用
			var numb = 12;
			alert(numb.toHexString());
			
			//重写对象已经存在的方法
			//把内容给打出来
			alert(createCar.toString());
			alert(window.onload.toString());
		}  			
		
  	</script>
  <body>
   
  </body>
</html>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics