js面向对象简单示例

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>JsObject.html</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <script>
    /* ---------------  定义类 ----------------*/
     function MyFirstObject(name,age)//这是java中的构造函数 
     {  
	      this.name = name;//this.name表示他是个public,公有属性   
	      this.age = age;   
	      var sex = 'm'; //性别吗当然要保密,所以他是个私有属性   
	      this.showPrivateProperty = function()//利用闭包机制实现对私有属性的访问   
	      {
	          alert(sex);   
	      }   
      }   
      MyFirstObject.prototype.show = function (){ //增加实例方法  
          alert(this.name+" "+this.age);   
      }
      MyFirstObject.prototype= { //增加实例方法  
            sayName:function(){
                alert(this.name);  
          }
      };
      //------------ 归类所有,不归实例所有 
      MyFirstObject.height="175cm"; //增加静态变量  
      MyFirstObject.showHeight=function(){//静态方法
           alert(MyFirstObject.height);
      }
      
	  var t = new MyFirstObject('campaign','25');   
	  //t.show(); //-->  campaign 25
	  t.sayName();//--> campaign
	  //alert(t.name);//-->campaign  == alert(t["name"]); 
	  //alert(t.sex);//-->undefined   
	  //t.showPrivateProperty();//-->m 
	  //alert(MyFirstObject.height);//-->175cm
	  //alert(t.height);//--> undefined
	  //MyFirstObject.showHeight();//-->175cm
	  //t.showHeight();//-->报错
	  
	  
	  /*---------------类继承  ----------------*/
	  //原型继承
	  function person1(){}
	  person1.prototype = new MyFirstObject('zhaozhi3758',23);
	  //alert(new person1().name);//-->zhaozhi3758
	  //alert(person1.height);//-->undefined
	  
	  //call()方法和apply()方法实现继承
	   function person2(name,age){
	       MyFirstObject.call(this,name,age);//--> ==MyFirstObject.call(this,[name,age]);
	   }
	   //new person2().showPrivateProperty();//m
	   
	   /*-------------   定义对象    -------------------*/
	   //---使用JavaScript中的Object类型
	   var  company= new Object();    
	   company.name= "天堂";   
	   company.address = "北京";   
	   company.produce= function(message)   
	   {   
	       alert(message);   
	   }   
       //company.produce("天堂");//-->天堂   
       
       //---利用json创建对象
       var myObj = {   
		       "name":"xiaoge",   
		       age:10,   
		       test:function(){   
                      alert("我叫"+this.name+"今年"+this.age+"岁");   
               }
               
       };   
       //myObj.test();
       //alert(myObj.age);
       // alert(myObj["age"]);
      
	   /*-------------  return  -------------------*/
	    var student=function(){
	          return{     
	               name:"zhaozhi3758",
	               init:function(){
	                  alert(this.name);
	               }
	           }    
	    }
        //new student().init();
        
        /*--------------- json   -------------------*/
        //遍历json字符串数组		
			var strJosn = "[{username:'guosheng',password:'123456'},{username:'hanhan',password:'123s'}]"; 		
			function setStr(){ 			
					strJosn = eval(strJosn);//(new Function("return ("+ strJson +")"))();//
				  for(var i=0; i<strJosn.length; i++)
				  {			  
				 	   alert(strJosn[i].username+strJosn[i].password);			
				  }		
		 	}   
		 	//setStr();   
        //----------------------------------------
          var json ={
                 name:"lennove",
                 price:4300,
                 config:{
                    cpu:"intel core 2",
                    memory:"2G"
                 }
          }
          //alert(json.price);
          //alert(json.config.cpu);
          //alert(json["config"]["cpu"]);
    </script>
  </body>
</html>

你可能感兴趣的:(JavaScript,html,json,prototype)