zoukankan      html  css  js  c++  java
  • js面向对象编程思想

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8" />
    		<title></title>
    	</head>
    	<body>
    		
    	</body>
    	<script>
    		//实例对象
    		var per1={
    			name:"卡卡西",
    			age:20,
    			sex:"男",
    			eat:function(){
    				console.log("吃拉面");
    			},
    			readBook:function(){
    				console.log("西游记");
    			}
    		}
    		//调用系统的构造函数创建对象
    		var per2=new Object();
    		per2.name="大蛇丸";
    		per2.age=30;
    		per2.sex="男";
    		per2.eat=function(){
    			console.log("吃榴莲");
    		};
    		per2.play=function(){
    			console.log("玩蛇");
    		}
    		//自定义构造函数
    		function Person(name,age,sex){
    			this.name=name;
    			this.age=age;
    			this.sex=sex;
    			this.play=function(){
    				console.log("天天打游戏");
    			};
    		}
    		
    		var per=new Person("小樱",18,"女");
    		console.log(per.name);
    		per.play();
    		
    		//工程模式创建对象
    		function creatObject(name,age){
    			var obj=Object();
    			obj.name=name;
    			obj.age=age;
    			obj.syHi=function(){
    				console.log("你好");
    			};
    			return obj;
    		}
    		
    		//自定义构造函数和工程模式区别
    		/**
    		 * 共同点:都是函数,都可以创建对象,都可以传入参数
    		 * 工厂:
    		 * 函数名是小写的
    		 * 有new
    		 * 有返回值
    		 * new之后的对象是当前的对象
    		 *直接调用函数就可以创建对象
    		 * 
    		 * 自定义构造函数:
    		 * 函数名是大写的首字母
    		 * 没有new
    		 * 没有返回值
    		 * this是当前对象
    		 * 通过new的方式创建对象
    		 * **/
    		
    		function Pers(name,age){
    			this.name=name;
    			this.age=age;
    		}
    		//通过原型来添加方法,解决数据共享,节省内存空间
    		Pers.prototype.eat=function(){
    			console.log("吃凉菜");
    		}
    		var p1=new Pers("小明",20);
    		var p2=new Pers("小红",30);
    		console.log(p1.eat==p2.eat);//结果为真
    	</script>
    </html>
    

      

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title></title>
    </head>
    <body>

    </body>
    <script>
    //实例对象
    var per1={
    name:"卡卡西",
    age:20,
    sex:"男",
    eat:function(){
    console.log("吃拉面");
    },
    readBook:function(){
    console.log("西游记");
    }
    }
    //调用系统的构造函数创建对象
    var per2=new Object();
    per2.name="大蛇丸";
    per2.age=30;
    per2.sex="男";
    per2.eat=function(){
    console.log("吃榴莲");
    };
    per2.play=function(){
    console.log("玩蛇");
    }
    //自定义构造函数
    function Person(name,age,sex){
    this.name=name;
    this.age=age;
    this.sex=sex;
    this.play=function(){
    console.log("天天打游戏");
    };
    }

    var per=new Person("小樱",18,"女");
    console.log(per.name);
    per.play();

    //工程模式创建对象
    function creatObject(name,age){
    var obj=Object();
    obj.name=name;
    obj.age=age;
    obj.syHi=function(){
    console.log("你好");
    };
    return obj;
    }

    //自定义构造函数和工程模式区别
    /**
    * 共同点:都是函数,都可以创建对象,都可以传入参数
    * 工厂:
    * 函数名是小写的
    * 有new
    * 有返回值
    * new之后的对象是当前的对象
    *直接调用函数就可以创建对象
    *
    * 自定义构造函数:
    * 函数名是大写的首字母
    * 没有new
    * 没有返回值
    * this是当前对象
    * 通过new的方式创建对象
    * **/

    function Pers(name,age){
    this.name=name;
    this.age=age;
    }
    //通过原型来添加方法,解决数据共享,节省内存空间
    Pers.prototype.eat=function(){
    console.log("吃凉菜");
    }
    var p1=new Pers("小明",20);
    var p2=new Pers("小红",30);
    console.log(p1.eat==p2.eat);//结果为真
    </script>
    </html>

  • 相关阅读:
    Leetcode Unique Binary Search Trees
    Leetcode Decode Ways
    Leetcode Range Sum Query 2D
    Leetcode Range Sum Query
    Leetcode Swap Nodes in Pairs
    Leetcode Rotate Image
    Leetcode Game of Life
    Leetcode Set Matrix Zeroes
    Leetcode Linked List Cycle II
    CF1321A
  • 原文地址:https://www.cnblogs.com/wordblog/p/10260429.html
Copyright © 2011-2022 走看看